473,378 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

export dataset to excel in winform

Hi!

I want to export a dataset to an excel file. I found following code on the
net...
( http://www.codeproject.com/csharp/Export.asp )

Excel.ApplicationClass excel = new ApplicationClass();

excel.Application.Workbooks.Add(true);
DataTable table = DATASETNAME.Tables[0];
int ColumnIndex=0;
foreach(Datacolumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[1,ColumnIndex]=col.ColumnName;
}
int rowIndex=0;
foreach(DataRow row in table.Row)
{
rowIndex++;
ColumnIndex=0;
foreach(DataColumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[rowIndex+1,ColumnIndex]=row.Cells[col.ColumnName].Text;

}
}
excel.Visible = true;
Worksheet worksheet = (Worksheet)excel.ActiveSheet;
worksheet.Activate();
)
I asume that this code will work just fine for me, but how do I include an
applicationclass for Excel?

Do I need to include an excel.dll file for this occasion, and if so where do
i find this dll fie?
---------------
Best regards
- Hans -
---------------
(Have fun programming with ... C#)
Nov 17 '05 #1
4 33420
After further research I think I need the

Microsoft Excel 11.0 Object Library.

Where do I find this
---------------
Best regards
- Hans -
---------------
(Have fun programming with ... C#)
"Hans [DiaGraphIT]" wrote:
Hi!

I want to export a dataset to an excel file. I found following code on the
net...
( http://www.codeproject.com/csharp/Export.asp )

Excel.ApplicationClass excel = new ApplicationClass();

excel.Application.Workbooks.Add(true);
DataTable table = DATASETNAME.Tables[0];
int ColumnIndex=0;
foreach(Datacolumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[1,ColumnIndex]=col.ColumnName;
}
int rowIndex=0;
foreach(DataRow row in table.Row)
{
rowIndex++;
ColumnIndex=0;
foreach(DataColumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[rowIndex+1,ColumnIndex]=row.Cells[col.ColumnName].Text;

}
}
excel.Visible = true;
Worksheet worksheet = (Worksheet)excel.ActiveSheet;
worksheet.Activate();
)
I asume that this code will work just fine for me, but how do I include an
applicationclass for Excel?

Do I need to include an excel.dll file for this occasion, and if so where do
i find this dll fie?
---------------
Best regards
- Hans -
---------------
(Have fun programming with ... C#)

Nov 17 '05 #2
Hi,

Take a look at this code, I'm using it and works great,
http://support.microsoft.com/default...b;EN-US;316934

Now, I do not need to create an excel table, I have embedded in my .EXE an
empty excel file with the format I need

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Hans [DiaGraphIT]" <ha********@sshf.no> wrote in message
news:89**********************************@microsof t.com...
Hi!

I want to export a dataset to an excel file. I found following code on
the
net...
( http://www.codeproject.com/csharp/Export.asp )

Excel.ApplicationClass excel = new ApplicationClass();

excel.Application.Workbooks.Add(true);
DataTable table = DATASETNAME.Tables[0];
int ColumnIndex=0;
foreach(Datacolumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[1,ColumnIndex]=col.ColumnName;
}
int rowIndex=0;
foreach(DataRow row in table.Row)
{
rowIndex++;
ColumnIndex=0;
foreach(DataColumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[rowIndex+1,ColumnIndex]=row.Cells[col.ColumnName].Text;

}
}
excel.Visible = true;
Worksheet worksheet = (Worksheet)excel.ActiveSheet;
worksheet.Activate();
)
I asume that this code will work just fine for me, but how do I include an
applicationclass for Excel?

Do I need to include an excel.dll file for this occasion, and if so where
do
i find this dll fie?
---------------
Best regards
- Hans -
---------------
(Have fun programming with ... C#)

Nov 17 '05 #3
Thank you for a good advice.

To write to My first thought was that the worksheet should be generic, and
build along the way, but writing to an already excisting excel file is
captivating idea.

I will consider this. The translation from VB to C# is noe problem but do
you have that code in c# already?

---------------
Best regards
- Hans -
---------------
(Have fun programming with ... C#)
"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

Take a look at this code, I'm using it and works great,
http://support.microsoft.com/default...b;EN-US;316934

Now, I do not need to create an excel table, I have embedded in my .EXE an
empty excel file with the format I need

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Hans [DiaGraphIT]" <ha********@sshf.no> wrote in message
news:89**********************************@microsof t.com...
Hi!

I want to export a dataset to an excel file. I found following code on
the
net...
( http://www.codeproject.com/csharp/Export.asp )

Excel.ApplicationClass excel = new ApplicationClass();

excel.Application.Workbooks.Add(true);
DataTable table = DATASETNAME.Tables[0];
int ColumnIndex=0;
foreach(Datacolumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[1,ColumnIndex]=col.ColumnName;
}
int rowIndex=0;
foreach(DataRow row in table.Row)
{
rowIndex++;
ColumnIndex=0;
foreach(DataColumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[rowIndex+1,ColumnIndex]=row.Cells[col.ColumnName].Text;

}
}
excel.Visible = true;
Worksheet worksheet = (Worksheet)excel.ActiveSheet;
worksheet.Activate();
)
I asume that this code will work just fine for me, but how do I include an
applicationclass for Excel?

Do I need to include an excel.dll file for this occasion, and if so where
do
i find this dll fie?
---------------
Best regards
- Hans -
---------------
(Have fun programming with ... C#)


Nov 17 '05 #4
Hi,

Hey, what about the lazyness? :)
I had to rewrite it myself too ;)

here is the piece of code I'm using to read from excel and writing to
access, it should be easy to modify to write in excel, just change the query

As I'm lazy too this morning I did not edited nothing, it goes straight from
my .cs

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

string srcConnString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\temp\crash data districtwide-2002.xls;Extended Properties=""Excel
8.0;HDR=YES;""";

string srcQuery = "Select * from [Sheet1$]";

OleDbConnection srcConn = new OleDbConnection( srcConnString);

srcConn.Open();

OleDbCommand objCmdSelect =new OleDbCommand( srcQuery, srcConn);

// OleDbDataAdapter da = new OleDbDataAdapter( srcQuery, srcConn);

// DataSet ds = new DataSet();

// da.Fill( ds);

OleDbDataReader reader = objCmdSelect.ExecuteReader(
CommandBehavior.CloseConnection);

//Target

string targetConnString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\temp\100test.mdb"; //;User Id=admin;Password=;"

OleDbConnection targetConn = new OleDbConnection( targetConnString);

targetConn.Open();

OleDbCommand targetCmd = new OleDbCommand();

targetCmd.Connection = targetConn;

targetCmd.CommandType = CommandType.Text;

targetCmd.CommandText = "INSERT INTO AllData " +

"( IDNUMBER , MILEPOST , KEYNUMBER , DATE , DAYOFWEEK , TIME , NUMBERCARS ,
NUMBERTRKS , " +

"ACCTYPE , SEVERITY , TRCNTRL03 , LITCOND04 , CHARCTR05 , SURFACE06 ,
WEATHER07 , CONTRIB19,"+

"CONTRIB20 , CONTRIB21 , CONTRIB22 , DIRECTN23 , DIRECTN24 , DIRECTVEH3 ,
DIRECTVEH4 , DESCRIPTN , SYMBOL ) " +

"VALUES ( "+

"@IDNUMBER , @MILEPOST , @KEYNUMBER , @DATE , @DAYOFWEEK , @TIME ,
@NUMBERCARS , @NUMBERTRKS , @ACCTYPE , " +

"@SEVERITY , @TRCNTRL03 , @LITCOND04 , @CHARCTR05 , @SURFACE06 , @WEATHER07
, @CONTRIB19 , @CONTRIB20 , "+

"@CONTRIB21 , @CONTRIB22 , @DIRECTN23 , @DIRECTN24 , @DIRECTVEH3 ,
@DIRECTVEH4 , @DESCRIPTN , @SYMBOL " +

")";
while( reader.Read() )

{

if ( reader[0] == System.DBNull.Value )

continue;

targetCmd.Parameters.Add( "@IDNUMBER", OleDbType.VarChar).Value = reader[0]
;

targetCmd.Parameters.Add( "@MILEPOST", OleDbType.Integer).Value = reader[8]
;

targetCmd.Parameters.Add( "@KEYNUMBER", OleDbType.Integer).Value =
1;//reader[2] ;

targetCmd.Parameters.Add( "@DATE", OleDbType.DBTimeStamp).Value = reader[1]
;

targetCmd.Parameters.Add( "@DAYOFWEEK", OleDbType.VarChar).Value =
((DateTime)reader[3]).DayOfWeek.ToString().Substring(0,2);

targetCmd.Parameters.Add( "@TIME", OleDbType.DBTimeStamp).Value = reader[2]
;

targetCmd.Parameters.Add( "@NUMBERCARS", OleDbType.Integer).Value =
reader[78] ;

targetCmd.Parameters.Add( "@NUMBERTRKS", OleDbType.Integer).Value =
0;//reader[7] ;

targetCmd.Parameters.Add( "@ACCTYPE", OleDbType.VarChar).Value = reader[8] ;
**********************************

targetCmd.Parameters.Add( "@SEVERITY", OleDbType.VarChar).Value = reader[9]
;

targetCmd.Parameters.Add( "@TRCNTRL03", OleDbType.Integer).Value =
reader[10] ;

targetCmd.Parameters.Add( "@LITCOND04", OleDbType.Integer).Value =
reader[11] ;

targetCmd.Parameters.Add( "@CHARCTR05", OleDbType.Integer).Value =
reader[12] ;

targetCmd.Parameters.Add( "@SURFACE06", OleDbType.Integer).Value =
reader[13] ;

targetCmd.Parameters.Add( "@WEATHER07", OleDbType.Integer).Value =
reader[14] ;

targetCmd.Parameters.Add( "@CONTRIB19", OleDbType.Integer).Value =
reader[15] ;

targetCmd.Parameters.Add( "@CONTRIB20", OleDbType.Integer).Value =
reader[16] ;

targetCmd.Parameters.Add( "@CONTRIB21", OleDbType.Integer).Value =
reader[17] ;

targetCmd.Parameters.Add( "@CONTRIB22", OleDbType.Integer).Value =
reader[18] ;

targetCmd.Parameters.Add( "@DIRECTN23", OleDbType.Integer).Value =
reader[19] ;

targetCmd.Parameters.Add( "@DIRECTN24", OleDbType.Integer).Value =
reader[20] ;

targetCmd.Parameters.Add( "@DIRECTVEH3", OleDbType.Integer).Value =
reader[21] ;

targetCmd.Parameters.Add( "@DIRECTVEH4", OleDbType.Integer).Value =
reader[22] ;

targetCmd.Parameters.Add( "@DESCRIPTN", OleDbType.VarChar).Value =
reader[23] ;

targetCmd.Parameters.Add( "@SYMBOL", OleDbType.VarChar).Value = reader[24] ;

targetCmd.ExecuteNonQuery();

}

targetConn.Close();

reader.Close();

srcConn.Close();

}

catch(Exception e1)

{

MessageBox.Show( e1.Message);

}
"Hans [DiaGraphIT]" <ha********@sshf.no> wrote in message
news:18**********************************@microsof t.com...
Thank you for a good advice.

To write to My first thought was that the worksheet should be generic, and
build along the way, but writing to an already excisting excel file is
captivating idea.

I will consider this. The translation from VB to C# is noe problem but do
you have that code in c# already?

---------------
Best regards
- Hans -
---------------
(Have fun programming with ... C#)
"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

Take a look at this code, I'm using it and works great,
http://support.microsoft.com/default...b;EN-US;316934

Now, I do not need to create an excel table, I have embedded in my .EXE
an
empty excel file with the format I need

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Hans [DiaGraphIT]" <ha********@sshf.no> wrote in message
news:89**********************************@microsof t.com...
> Hi!
>
> I want to export a dataset to an excel file. I found following code on
> the
> net...
> ( http://www.codeproject.com/csharp/Export.asp )
>
> Excel.ApplicationClass excel = new ApplicationClass();
>
> excel.Application.Workbooks.Add(true);
> DataTable table = DATASETNAME.Tables[0];
> int ColumnIndex=0;
> foreach(Datacolumn col in table.Columns)
> {
> ColumnIndex++;
> excel.Cells[1,ColumnIndex]=col.ColumnName;
> }
> int rowIndex=0;
> foreach(DataRow row in table.Row)
> {
> rowIndex++;
> ColumnIndex=0;
> foreach(DataColumn col in table.Columns)
> {
> ColumnIndex++;
>
> excel.Cells[rowIndex+1,ColumnIndex]=row.Cells[col.ColumnName].Text;
>
> }
> }
> excel.Visible = true;
> Worksheet worksheet = (Worksheet)excel.ActiveSheet;
> worksheet.Activate();
> )
>
>
> I asume that this code will work just fine for me, but how do I include
> an
> applicationclass for Excel?
>
> Do I need to include an excel.dll file for this occasion, and if so
> where
> do
> i find this dll fie?
>
>
> ---------------
> Best regards
> - Hans -
> ---------------
> (Have fun programming with ... C#)


Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Maria L. | last post by:
Hi, I need to export the content of a DataGrid (in Windows application in C#), into an Excel spreadsheet. Anyone knows how to do this? Any code snippets would help! thanks a lot, Maria
1
by: Kevin Blakeley | last post by:
I know this was just posted but I did not want this message to get lost in the other thread as it's slightly different. Yes I want to export my dataset to excel for my clients, but I don't want...
1
by: ad | last post by:
I used the code below to export a table in a dataset to Excel. It can export only on table at a time. Can export more than on table in a dataset to Excel public static void...
4
by: John Z. | last post by:
I have read numerous articles and postings on various approaches to exporting a DataSet to excel while avoiding the use of automation. I found the most performant approach to be assigning a...
3
by: Scott M. Lyon | last post by:
I'm trying to figure out a way to export data (actually the result of a Stored Procedure call from SQL Server) into a specified Excel spreadsheet format. Currently, I have the data read into a...
3
by: Sachin Salgarkar | last post by:
I have a DataSet that I need to export to Excel. The dataset has multiple tables. I need a way to export the complete dataset to a single Excel Workbook with sheets for each table in the dataset....
1
by: DennisBetten | last post by:
First of all, I need to give some credit to Mahesh Chand for providing me with an excellent basis to export data to excel. What does this code do: As the title says, this code is capable of...
4
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I've got a DataSet that I save as XML using the DataSet DataTable's WriteXml method. If I say XmlWriteMode.IgnoreSchema, it shows up great in Excel, but I can not reopen the file in my...
2
hemantbasva
by: hemantbasva | last post by:
Note We need to have a template on server for generating report in multiple sheet as we do not had msoffice on server moreover this require a batch job to delete excel file created by the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.