Back in February I wrote a post on how to export DataTables to XML and Excel and I still get a lot of search engine traffic to that post.
People have been asking me to simplify the example and only concentrate on the Excel export, so that's what I will do now.
All spreadsheet applications (Excel, Calc etc.) understand semicolon separated files natively, so everyone can use this method - you don't even have to use Excel for it to work.
public static void ExportToSpreadsheet(DataTable table, string name)
{
HttpContext context = HttpContext.Current;
context.Response.Clear();
foreach (DataColumn column in table.Columns)
{
context.Response.Write(column.ColumnName + ";");
}
context.Response.Write(Environment.NewLine);
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
context.Response.Write(row[i].ToString().Replace(";", string.Empty) + ";");
}
context.Response.Write(Environment.NewLine);
}
context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name + ".csv");
context.Response.End();
}
Then just call this method and pass the DataTable and the filename as parameters.
ExportToSpreadsheet(table, "products");
The method is static so you can use it anywhere in a web application. Put it on a page, a HTTP Handler, add it to the App_Code folder or stick it in a separate assembly. As long as you call it from a web application it will work.
Tag:
Add to Del.icio.us | Digg | Reddit | Furl
Bookmark WebProNews:
Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.
http://www.madskristensen.dk/
About the author:
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.
http://www.madskristensen.dk/
Comments
Works great!
Works great! Odd to me that you used semi-colons as your delimter instead of surrounding text in quotes and using comma as delimiter. But I was able to easily translate. Are .csv delimters different here in America?
export to excel
hw to export more than 200 columns and 15000 rows . it takin more tha 20 mins
This works really well - way
This works really well - way better than all of the other options out there. Thanks!
good post!
good post!
hi, Do you have a visual
hi,
Do you have a visual basic code for this? Im new at .net and i wana create a class that will be exporting to excel a datatable. Exactly how this code of your do. But only using visual basic codes.
Thanks a lot...
ron
Should look like
Should look like this:
Public Shared Sub ExportToSpreadsheet(ByVal table As DataTable, ByVal name As String) Dim context As HttpContext = HttpContext.Current context.Response.Clear() For Each column As DataColumn In table.Columns context.Response.Write(column.ColumnName + ";") Next context.Response.Write(Environment.NewLine) For Each row As DataRow In table.Rows For i As Integer = 0 To table.Columns.Count - 1 context.Response.Write(row(i).ToString().Replace(";", String.Empty) + ";") Next context.Response.Write(Environment.NewLine) Next context.Response.ContentType = "text/csv" context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name + ".csv") context.Response.[End]() End SubPost new comment