You can use ExportDataTable method to export a generic datatable content to a CSV format.
The following sample is used in ASP.NET context to export the result of a gridview. This code-sample should be place in the handler method of a click button event.
protected void LinkButtonExportAsExcel_Click(object sender, EventArgs e)
{
try
{
DataView dataView = (DataView)Cache["DataSource"];
if (dataView == null) dataView = GetDataView();
char[] bufferedExport = ExportDataTable(dataView.Table, "CSV");
if (bufferedExport == null) return;
Response.Clear();
Response.ContentType = "text/csv";
Response.ContentEncoding = Encoding.Default;
Response.Charset = Encoding.Default.EncodingName;
Response.AddHeader("Content-Disposition", "attachment;filename=Export.csv");
Response.AddHeader("Content-Length",
Encoding.Default.GetByteCount(bufferedExport).ToString());
Response.BinaryWrite(Encoding.Default.GetBytes(b...
[More]
e4b576cc-4d76-4c79-9af9-106b1c85ec13|0|.0
C# sample code, Office
C# sample code, Office