April 18, 2016 21:24 by Dan
Today while writing a small web application where I needed to download a datatable as csv file. I faced issue of page html getting wrongly appended to file being downloaded. I was using Response.Write to download file on client machine.
Solution to my problem was easy, I just needed to add an extra line telling content-length. Below is method that I finally ended up using.
private void DownloadDataAsCsv(DataTable dt)
string tab = "";
StringBuilder sb = new StringBuilder();
foreach (DataColumn dc in dt.Columns)
sb.Append(tab + dc.ColumnName);
tab = ",";
foreach (DataRow dr in dt.Rows)
tab = "";
for (i = 0; i < dt.Columns.Count; i++)
sb.Append(tab + dr[i].ToString());
tab = ",";
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment; filename=Export.csv");
Best Windows Shared Hosting Recommendation
ASPHostPortal.com provides our customers with Plesk Panel, one of the most popular and stable control panels for Windows hosting, as free. You could also see the latest .NET framework, a crazy amount of functionality as well as Large disk space, bandwidth, MSSQL databases and more. All those give people the convenience to build up a powerful site in Our Shared Hosting. We offers Windows hosting starts from $5/month only. We also guarantees 30 days money back and guarantee 99.9% uptime. If you need a reliable affordable Windows Shared Hosting, we should be your best choice.