C# .NET - Exporting dataTable to Excel sheet

Asked By san san on 02-Dec-08 09:47 AM
Hi all

I want to write/export a dataTable to an excel sheet.
If I use Interop.dll/excel.dll the target machine should be installed with the excel software.
If I use interop.dll , this may lead to a license issue.
Butin my case, the target machine need not to be installed with the excel software.
I need to run my app in all the machins including one without excel software.
If i go for ADO.NET, I get an error "The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine".
and i set the target platform to x86.. but still i have the same error...
I don't want to go with x86/ 32 bit platform...

Any better idea on exporting a dataTable to excel sheet.
this should be fair and simple.
I need a possible best solution.

Any help would be greately appriciated.

Thanks
SAN

solution

Perry replied to san san on 02-Dec-08 11:02 AM

>>Any better idea on exporting a dataTable to excel sheet.

Please find the complete code in C# at http://www.eggheadcafe.com/forumpost.aspx?topicid=2&forumpostid=3954 The user has given it and works fine.

Alternativelly you can find below code that can be use directly:

http://www.mail-archive.com/techdotnetindia@googlegroups.com/msg00020.html

http://www.webpronews.com/expertarticles/2006/11/28/aspnet-export-a-datatable-to-excel

http://wiki.apache.org/myfaces/Exporting_DataTable_To_MS-Excel

one more

Perry replied to san san on 02-Dec-08 11:04 AM
http://www.codeproject.com/KB/vb/Senthil_S__Software_Eng_.aspx => just download the zip source code file. You can also use demo executable.

export datatable to excel

Binny ch replied to san san on 02-Dec-08 11:37 AM

Imports Excel.XlFileFormat

Imports System.Data

Imports System.Data.SqlClient

Imports System.Data.SqlTypes

Imports System.IO


Public Function sqltabletocsvorxls(ByVal dt As DataTable, ByRef strpath As
String, ByVal dtype As String, ByVal includeheader As Boolean) As Integer

' signature:

' dim funcs as new imcfunctionlib.functions

' dim xint as integer

' xint = funcs.sqltabletocsvorxls(dsmanifest.tables(0),mstrpath,
"csv",false)

' where mstrpath = , say, "f:\imcapps\xlsfiles\test.xls"

sqltabletocsvorxls = 0

Dim objxl As Excel.Application

Dim objwbs As Excel.Workbooks

Dim objwb As Excel.Workbook

Dim objws As Excel.Worksheet

Dim mrow As DataRow

Dim colindex As Integer

Dim rowindex As Integer

Dim col As DataColumn

Dim fi As FileInfo = New FileInfo(strpath)

If fi.Exists = True Then

Kill(strpath)

End If

objxl = New Excel.Application

'objxl.Visible = False ' i may not need to do this

objwbs = objxl.Workbooks

objwb = objwbs.Add

objws = CType(objwb.Worksheets(1), Excel.Worksheet)

' i many want to change this to pass in a variable to determine

' if i want to have a column name row or not

If includeheader Then

For Each col In dt.Columns

colindex += 1

objws.Cells(1, colindex) = col.ColumnName

Next

rowindex = 1

Else

rowindex = 0

End If

For Each mrow In dt.Rows

rowindex += 1

colindex = 0

For Each col In dt.Columns

colindex += 1

objws.Cells(rowindex, colindex) = mrow(col.ColumnName).ToString()

Next

Next

If dtype = "csv" Then

objwb.SaveAs(strpath, xlCSV)

Else

objwb.SaveAs(strpath)

End If

objxl.DisplayAlerts = False

objws.Close()

objxl.DisplayAlerts = True

Marshal.ReleaseComObject(objws)

objxl.Quit()

Marshal.ReleaseComObject(objxl)

objws = Nothing

objwb = Nothing

objwbs = Nothing

objxl = Nothing

End Function

reply
Binny ch replied to san san on 02-Dec-08 11:39 AM

a better way is by using Excel Interop

http://www.csharphelp.com/board2/read.html?f=1&i=54657&t=54657
http://www.codeproject.com/KB/cs/Simple_Excel_Automation.aspx1:


 Excel.Application excelApp = new Excel.ApplicationClass();
excelApp.DisplayAlerts = false;
try
{
Excel.Workbook excelWkBook = excelApp.Workbooks._Open(
filename, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows,
"", true, false, 0, true);
 
Excel.Worksheet worksheet = (Excel.Worksheet)excelWkBook.ActiveSheet;
 
Range rg = worksheet.get_Range("A1","E1");
rg.Select();
rg.Font.Bold = true;
rg.Font.Name = "Arial";
rg.Font.Size = 10;
rg.WrapText = true;
rg.HorizontalAlignment = Excel.Constants.xlCenter;
rg.Interior.ColorIndex = 40;
rg.Borders.Weight = 3;
rg.Borders.LineStyle = Excel.Constants.xlSolid;
rg.Cells.RowHeight = 38;
 

try this
C_A P replied to san san on 03-Dec-08 04:26 AM
It's going to be slow compared to native VBA code. There are a few ways to
do thiis...the best of which is to query the http://www.pcreview.co.uk/forums/thread-1237845.php# directly from Excel.
If you check out http://www.aspnetpro.com/ (October 2003) they discuss Exporting to
Excel in depth, albiet primarily from the perspective of http://www.pcreview.co.uk/forums/thread-1237845.php#.

Here's another link...

http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q319180

or

u can try generating http://www.pcreview.co.uk/forums/thread-1237845.php# and provide link to CSV file on the web page,
but the http://www.pcreview.co.uk/forums/thread-1237845.php# need to have mapped the CSV file to Excel


try this link
C_A P replied to san san on 03-Dec-08 04:30 AM

http://nishantpant.wordpress.com/2006/11/02/exporting-data-to-excel-from
http://www.aspnetpro.com/NewsletterArticle/2003/09/asp200309so_l/asp200309so_l.asp
http://www.webpronews.com/expertarticles/2006/11/28/aspnet-export-a-datatable-to-excel


Cika Pero replied to san san on 04-Nov-11 05:03 AM
Hi,

you could try this http://www.gemboxsoftware.com/spreadsheet/overview component to export http://www.gemboxsoftware.com/support/articles/import-export-datatable-xls-xlsx-ods-csv-html-net.
Here is a sample http://www.gemboxsoftware.com/spreadsheet/overview code how to export entire DataSet to Excel:
var ef = new ExcelFile();
 
foreach (DataTable dataTable in dataSet.Tables)
  ef.Worksheets.Add(dataTable.TableName).InsertDataTable(dataTable, 0, 0, true);
 
ef.SaveXls(dataSet.DataSetName + ".xls");