I don’t do (or particularly like) ASP.Net development often but recently had to quickly add some ‘Export to CSV’ functionality of a displayed table to a page. Personally I would have just used ‘copy and paste’ to do it (the export) but since the user isn’t in the habit of thinking for themselves I had to create new functionality which turn out to be very nice – and even opens the door for some future more advanced functionality.
Anyway, to achieve this is really easy if you use the HttpContext.Current.Response class and add an attachment ‘header’ to your page. The header must be named ‘content-disposition‘ and ContentType ‘text/csv‘. See the following methods I created to simply take a DataSet and convert it to ‘CSV file’:
public class CSVExportUtils
{
public void ExportDataSetToCSV(string title, DataSet ds, string columnNamesCSV = "")
{
string attachment = "attachment; filename=" + title.Replace(",", "").Replace(".", "") + ".csv";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Pragma", "public");
if (columnNamesCSV.Length == 0)
{
WriteColumnName(ds);
foreach (DataRow r in ds.Tables[0].Rows)
{
if (ds.Tables[0].Columns.Count > 0)
{
string value = r[0].ToString();
if (value.Contains(","))
value = "\"" + value + "\"";
HttpContext.Current.Response.Write(value);
for (int i = 1; i < ds.Tables[0].Columns.Count; i++)
{
value = r[i].ToString();
if (value.Contains(","))
value = "\"" + value + "\"";
HttpContext.Current.Response.Write("," + value);
}
}
HttpContext.Current.Response.Write(Environment.NewLine);
}
}
else
{
HttpContext.Current.Response.Write(columnNamesCSV);
HttpContext.Current.Response.Write(Environment.NewLine);
string[] columns = columnNamesCSV.Split(',');
foreach (DataRow r in ds.Tables[0].Rows)
{
string value = r[columns[0]].ToString();
if (value.Contains(","))
value = "\"" + value + "\"";
HttpContext.Current.Response.Write(value);
for (int i = 1; i < columns.Length; i++)
{
value = r[columns[i]].ToString();
if (value.Contains(","))
value = "\"" + value + "\"";
HttpContext.Current.Response.Write("," + value);
}
HttpContext.Current.Response.Write(Environment.NewLine);
}
}
HttpContext.Current.Response.End();
}
private static void WriteColumnName(DataSet ds)
{
StringBuilder sb = new StringBuilder();
if (ds.Tables[0].Columns.Count > 0)
{
string value = ds.Tables[0].Columns[0].Caption;
if (value.Contains(","))
value = "\"" + value + "\"";
HttpContext.Current.Response.Write(value);
for (int i = 1; i < ds.Tables[0].Columns.Count; i++)
{
value = ds.Tables[0].Columns[i].Caption;
if (value.Contains(","))
value = "\"" + value + "\"";
HttpContext.Current.Response.Write("," + value);
}
}
HttpContext.Current.Response.Write(Environment.NewLine);
}
}
This method also allows you to specify the fields you want to export – or alternatively it just use the ones in the DataSet. For example, you can use it like this:
if (Request.QueryString["csvexport"] != null)
{
CSVExportUtils csvExportUtils = new CSVExportUtils();
csvExportUtils.ExportDataSetToCSV(fileName, dataSet, "Field1,Field2,...");
}
The calling page can then simply have a hyperlink like this:
SomePage.aspx?csvexport=Yes

