Here's a really quick tip: how to convert a DataTable to CSV, and write it dynamically to the response stream.
In ASP.NET, If you need to allow users to download the contents of a datatable in flat file format (i.e. CSV, TAB etc) you could do this by writing the data to a temporary file, then writing the resulting file to the response using TransmitFile. However, a quicker and less expensive method is to stream it directly. Here's a method which allows you to do just that:
And here's an example of calling the above method with some test data:

In ASP.NET, If you need to allow users to download the contents of a datatable in flat file format (i.e. CSV, TAB etc) you could do this by writing the data to a temporary file, then writing the resulting file to the response using TransmitFile. However, a quicker and less expensive method is to stream it directly. Here's a method which allows you to do just that:
/// <summary> /// Writes a datatable in delimited file format to the response stream. /// </summary> /// <param name="dt"></param> /// <param name="fileName"></param> /// <param name="delimiter"></param> private void WriteDelimitedData(DataTable dt, string fileName, string delimiter) { //prepare the output stream Response.Clear(); Response.ContentType = "text/csv"; Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}", fileName)); //write the csv column headers for (int i = 0; i < dt.Columns.Count; i++) { Response.Write(dt.Columns[i].ColumnName); Response.Write((i < dt.Columns.Count - 1) ? delimiter : Environment.NewLine); } //write the data foreach (DataRow row in dt.Rows) { for (int i = 0; i < dt.Columns.Count; i++) { Response.Write(row[i].ToString()); Response.Write((i < dt.Columns.Count - 1) ? delimiter : Environment.NewLine); } } Response.End(); }
And here's an example of calling the above method with some test data:
//create a datatable to hold the test data DataTable dt = new DataTable(); dt.Columns.Add("Column 1", typeof(string)); dt.Columns.Add("Column 2", typeof(string)); //generate some random data in the datatable Random rnd = new Random(); for (int i = 0; i < 100; i++) { dt.Rows.Add(rnd.Next(1, 1000000).ToString(), rnd.Next(1, 1000000).ToString()); } this.WriteDelimitedData(dt, "testdata.csv", ",");
Quick, and easy

Feedback
# re: ASP.NET: Downloading a DataTable in CSV Format
Thanks for the great code. 11/13/2009 9:33 AM | Wealthy Affiliate# re: ASP.NET: Downloading a DataTable in CSV Format
Good looking code but will this increase your site traffic/utilization stats? I guess not, the file is the same size regardless of delivery. 1/18/2010 3:15 AM | Charles# re: ASP.NET: Downloading a DataTable in CSV Format
@Charles: No, not as it stands. However, you can look into compressing your HTTP responses, and this will decrease the amount of data you need to send. Have a look at this article for more info:http://www.west-wind.com/Weblog/posts/10564.aspx 1/18/2010 3:52 AM | adampooler
# re: ASP.NET: Downloading a DataTable in CSV Format
Great little method, just put it into play today.My small modification:
if (row[i].ToString().IndexOf(",") > -1)
{
Response.Write("\"" + row[i].ToString() + "\"");
}
else
{
Response.Write(row[i].ToString());
}
Which handles if there is a comma in the data of a given column.
No comments:
Post a Comment