473,545 Members | 4,241 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Applicati onClass excel = new ApplicationClas s();

excel.Applicati on.Workbooks.Ad d(true);
DataTable table = DATASETNAME.Tab les[0];
int ColumnIndex=0;
foreach(Datacol umn col in table.Columns)
{
ColumnIndex++;
excel.Cells[1,ColumnIndex]=col.ColumnName ;
}
int rowIndex=0;
foreach(DataRow row in table.Row)
{
rowIndex++;
ColumnIndex=0;
foreach(DataCol umn col in table.Columns)
{
ColumnIndex++;
excel.Cells[rowIndex+1,Colu mnIndex]=row.Cells[col.ColumnName].Text;

}
}
excel.Visible = true;
Worksheet worksheet = (Worksheet)exce l.ActiveSheet;
worksheet.Activ ate();
)
I asume that this code will work just fine for me, but how do I include an
applicationclas s 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 33445
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.Applicati onClass excel = new ApplicationClas s();

excel.Applicati on.Workbooks.Ad d(true);
DataTable table = DATASETNAME.Tab les[0];
int ColumnIndex=0;
foreach(Datacol umn col in table.Columns)
{
ColumnIndex++;
excel.Cells[1,ColumnIndex]=col.ColumnName ;
}
int rowIndex=0;
foreach(DataRow row in table.Row)
{
rowIndex++;
ColumnIndex=0;
foreach(DataCol umn col in table.Columns)
{
ColumnIndex++;
excel.Cells[rowIndex+1,Colu mnIndex]=row.Cells[col.ColumnName].Text;

}
}
excel.Visible = true;
Worksheet worksheet = (Worksheet)exce l.ActiveSheet;
worksheet.Activ ate();
)
I asume that this code will work just fine for me, but how do I include an
applicationclas s 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********@ssh f.no> wrote in message
news:89******** *************** ***********@mic rosoft.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.Applicati onClass excel = new ApplicationClas s();

excel.Applicati on.Workbooks.Ad d(true);
DataTable table = DATASETNAME.Tab les[0];
int ColumnIndex=0;
foreach(Datacol umn col in table.Columns)
{
ColumnIndex++;
excel.Cells[1,ColumnIndex]=col.ColumnName ;
}
int rowIndex=0;
foreach(DataRow row in table.Row)
{
rowIndex++;
ColumnIndex=0;
foreach(DataCol umn col in table.Columns)
{
ColumnIndex++;
excel.Cells[rowIndex+1,Colu mnIndex]=row.Cells[col.ColumnName].Text;

}
}
excel.Visible = true;
Worksheet worksheet = (Worksheet)exce l.ActiveSheet;
worksheet.Activ ate();
)
I asume that this code will work just fine for me, but how do I include an
applicationclas s 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********@ssh f.no> wrote in message
news:89******** *************** ***********@mic rosoft.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.Applicati onClass excel = new ApplicationClas s();

excel.Applicati on.Workbooks.Ad d(true);
DataTable table = DATASETNAME.Tab les[0];
int ColumnIndex=0;
foreach(Datacol umn col in table.Columns)
{
ColumnIndex++;
excel.Cells[1,ColumnIndex]=col.ColumnName ;
}
int rowIndex=0;
foreach(DataRow row in table.Row)
{
rowIndex++;
ColumnIndex=0;
foreach(DataCol umn col in table.Columns)
{
ColumnIndex++;
excel.Cells[rowIndex+1,Colu mnIndex]=row.Cells[col.ColumnName].Text;

}
}
excel.Visible = true;
Worksheet worksheet = (Worksheet)exce l.ActiveSheet;
worksheet.Activ ate();
)
I asume that this code will work just fine for me, but how do I include an
applicationclas s 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=Micr osoft.Jet.OLEDB .4.0;Data
Source=C:\temp\ crash data districtwide-2002.xls;Extend ed Properties=""Ex cel
8.0;HDR=YES;""" ;

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

OleDbConnection srcConn = new OleDbConnection ( srcConnString);

srcConn.Open();

OleDbCommand objCmdSelect =new OleDbCommand( srcQuery, srcConn);

// OleDbDataAdapte r da = new OleDbDataAdapte r( srcQuery, srcConn);

// DataSet ds = new DataSet();

// da.Fill( ds);

OleDbDataReader reader = objCmdSelect.Ex ecuteReader(
CommandBehavior .CloseConnectio n);

//Target

string targetConnStrin g = @"Provider=Micr osoft.Jet.OLEDB .4.0;Data
Source=c:\temp\ 100test.mdb"; //;User Id=admin;Passwo rd=;"

OleDbConnection targetConn = new OleDbConnection ( targetConnStrin g);

targetConn.Open ();

OleDbCommand targetCmd = new OleDbCommand();

targetCmd.Conne ction = targetConn;

targetCmd.Comma ndType = CommandType.Tex t;

targetCmd.Comma ndText = "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.V alue )

continue;

targetCmd.Param eters.Add( "@IDNUMBER" , OleDbType.VarCh ar).Value = reader[0]
;

targetCmd.Param eters.Add( "@MILEPOST" , OleDbType.Integ er).Value = reader[8]
;

targetCmd.Param eters.Add( "@KEYNUMBER ", OleDbType.Integ er).Value =
1;//reader[2] ;

targetCmd.Param eters.Add( "@DATE", OleDbType.DBTim eStamp).Value = reader[1]
;

targetCmd.Param eters.Add( "@DAYOFWEEK ", OleDbType.VarCh ar).Value =
((DateTime)read er[3]).DayOfWeek.ToS tring().Substri ng(0,2);

targetCmd.Param eters.Add( "@TIME", OleDbType.DBTim eStamp).Value = reader[2]
;

targetCmd.Param eters.Add( "@NUMBERCAR S", OleDbType.Integ er).Value =
reader[78] ;

targetCmd.Param eters.Add( "@NUMBERTRK S", OleDbType.Integ er).Value =
0;//reader[7] ;

targetCmd.Param eters.Add( "@ACCTYPE", OleDbType.VarCh ar).Value = reader[8] ;
*************** *************** ****

targetCmd.Param eters.Add( "@SEVERITY" , OleDbType.VarCh ar).Value = reader[9]
;

targetCmd.Param eters.Add( "@TRCNTRL03 ", OleDbType.Integ er).Value =
reader[10] ;

targetCmd.Param eters.Add( "@LITCOND04 ", OleDbType.Integ er).Value =
reader[11] ;

targetCmd.Param eters.Add( "@CHARCTR05 ", OleDbType.Integ er).Value =
reader[12] ;

targetCmd.Param eters.Add( "@SURFACE06 ", OleDbType.Integ er).Value =
reader[13] ;

targetCmd.Param eters.Add( "@WEATHER07 ", OleDbType.Integ er).Value =
reader[14] ;

targetCmd.Param eters.Add( "@CONTRIB19 ", OleDbType.Integ er).Value =
reader[15] ;

targetCmd.Param eters.Add( "@CONTRIB20 ", OleDbType.Integ er).Value =
reader[16] ;

targetCmd.Param eters.Add( "@CONTRIB21 ", OleDbType.Integ er).Value =
reader[17] ;

targetCmd.Param eters.Add( "@CONTRIB22 ", OleDbType.Integ er).Value =
reader[18] ;

targetCmd.Param eters.Add( "@DIRECTN23 ", OleDbType.Integ er).Value =
reader[19] ;

targetCmd.Param eters.Add( "@DIRECTN24 ", OleDbType.Integ er).Value =
reader[20] ;

targetCmd.Param eters.Add( "@DIRECTVEH 3", OleDbType.Integ er).Value =
reader[21] ;

targetCmd.Param eters.Add( "@DIRECTVEH 4", OleDbType.Integ er).Value =
reader[22] ;

targetCmd.Param eters.Add( "@DESCRIPTN ", OleDbType.VarCh ar).Value =
reader[23] ;

targetCmd.Param eters.Add( "@SYMBOL", OleDbType.VarCh ar).Value = reader[24] ;

targetCmd.Execu teNonQuery();

}

targetConn.Clos e();

reader.Close();

srcConn.Close() ;

}

catch(Exception e1)

{

MessageBox.Show ( e1.Message);

}
"Hans [DiaGraphIT]" <ha********@ssh f.no> wrote in message
news:18******** *************** ***********@mic rosoft.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********@ssh f.no> wrote in message
news:89******** *************** ***********@mic rosoft.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.Applicati onClass excel = new ApplicationClas s();
>
> excel.Applicati on.Workbooks.Ad d(true);
> DataTable table = DATASETNAME.Tab les[0];
> int ColumnIndex=0;
> foreach(Datacol umn col in table.Columns)
> {
> ColumnIndex++;
> excel.Cells[1,ColumnIndex]=col.ColumnName ;
> }
> int rowIndex=0;
> foreach(DataRow row in table.Row)
> {
> rowIndex++;
> ColumnIndex=0;
> foreach(DataCol umn col in table.Columns)
> {
> ColumnIndex++;
>
> excel.Cells[rowIndex+1,Colu mnIndex]=row.Cells[col.ColumnName].Text;
>
> }
> }
> excel.Visible = true;
> Worksheet worksheet = (Worksheet)exce l.ActiveSheet;
> worksheet.Activ ate();
> )
>
>
> I asume that this code will work just fine for me, but how do I include
> an
> applicationclas s 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
3751
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
3364
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 it to show in the browser. Everything I have tried makes it so that IE will load excel within the browser session and view the file in there. What...
1
1465
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 Convert(System.Data.DataSet ds, int TableIndex, System.Web.HttpResponse response) { if (TableIndex > ds.Tables.Count - 1) {
4
12029
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 string array of data to a range in excel (similar to a copyfromrecordset). However, with this approach, the datatype is lost and all numeric columns in...
3
11674
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 DataSet that consists of one DataTable. That DataTable has a specific name (for arguments purposes, let's say it's called "DataGrid"), that I...
3
2965
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. Is that possible ? I have seen solutions that export a single table to an Excel file. Opening the XML ( DataSet.getXML() ) in Excel is a...
1
6666
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 extracting all tables and it's data from any given database! I was searching the net for a program like this, but I didn't come accross any (free)...
4
9590
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 application because there is no schema for it. On the other hand, I can choose XmlWriteMode.WriteSchema, and the data in Excel looks more like some kind of...
2
6393
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 method.... it creates 6 sheets # region Namespaces using System;
0
7396
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7751
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5323
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1874
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.