StringBuilder sb = new StringBuilder();
DataTable dt = ds.Tables[0];
IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>().Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in dt.Rows)
{
IEnumerable<string> fields = row
.ItemArray
.Select(field => string
.Concat("\"", field
.ToString()
.Replace("\"", "\"\""), "\""));
sb.AppendLine(string.Join(",", fields));
}
Response.Clear();
Response.Buffer = true;
Response.AddHeader("Content-Disposition", "inline;filename=export.csv");
Response.AddHeader("Content-Type", "application/Excel");
Response.ContentType = "text/csv";
Response.Write(sb);
Response.End();
DataTable dt = ds.Tables[0];
IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>().Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in dt.Rows)
{
IEnumerable<string> fields = row
.ItemArray
.Select(field => string
.Concat("\"", field
.ToString()
.Replace("\"", "\"\""), "\""));
sb.AppendLine(string.Join(",", fields));
}
Response.Clear();
Response.Buffer = true;
Response.AddHeader("Content-Disposition", "inline;filename=export.csv");
Response.AddHeader("Content-Type", "application/Excel");
Response.ContentType = "text/csv";
Response.Write(sb);
Response.End();
No comments:
Post a Comment