using System;
using System.Data;
using System.IO;
using System.Web;
public static class ExportHelper
{
public static void ExportDataTableToCsv(HttpContext context, DataTable table, string filename)
{
int num2;
context.Response.Clear();
context.Response.Buffer = true;
context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
context.Response.Charset = "";
StreamWriter writer = new StreamWriter(context.Response.OutputStream);
int count = table.Columns.Count;
for (num2 = 0; num2 <= (count - 1); num2++)
{
writer.Write(table.Columns[num2]);
if (num2 < (count - 1))
{
writer.Write(",");
}
}
writer.Write(writer.NewLine);
num2 = 0;
foreach (DataRow row in table.Rows)
{
for (num2 = 0; num2 <= (count - 1); num2++)
{
if (!Convert.IsDBNull(row[num2]))
{
writer.Write("\"");
writer.Write(row[num2].ToString());
writer.Write("\"");
}
else
{
writer.Write("");
}
if (num2 < (count - 1))
{
writer.Write(",");
}
}
writer.Write(writer.NewLine);
}
writer.Close();
context.Response.End();
}
}