System.Text.StringBuilder sb = new System.Text.StringBuilder();
//sb 中定义生成.csv文件的格式 中间以"\t"分开
Response.AddHeader("Content-Disposition", "attachment; filename=vhost" + string.Format("{0:yyyyMMddhhmmss}", DateTime.Now) + ".xls");
Response.ContentType = "application/text";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sb);
Response.End();
//sb 中定义生成.csv文件的格式 中间以"\t"分开
Response.AddHeader("Content-Disposition", "attachment; filename=vhost" + string.Format("{0:yyyyMMddhhmmss}", DateTime.Now) + ".xls");
Response.ContentType = "application/text";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sb);
Response.End();
//convert the dataset to CSV
StringBuilder sb = new StringBuilder();
for (int i = 0; i < this.GridViewSearchResult.Columns.Count; i++)
{
if (this.GridViewSearchResult.Columns[i].Visible)
{
sb.Append(@"""" + this.GridViewSearchResult.Columns[i].HeaderText + @"""" + ",");
}
}
sb.Append("\n");
foreach (DataRow row in ds.Tables[0].Rows)
{
for (int j = 0; j < this.GridViewSearchResult.Columns.Count;j++ )
{
sb.Append(@""""+row[this.GridViewSearchResult.Columns[j].HeaderText ].ToString().Replace(@"""",@"""""")+ @"""" + ",");
}
sb.Append("\n");
}
sb.Remove(sb.Length - 2, 1);
//save CSV to client
this.Response.Clear();
this.Response.ContentEncoding = System.Text.Encoding.Default;
this.Response.ContentType = "test/csv";
this.Response.AppendHeader("content-disposition", "attachment; filename=" + string.Format("{0:yyyyMMddhhmmss}", DateTime.Now) + ".csv");
this.Response.Write(sb);
this.Response.End();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < this.GridViewSearchResult.Columns.Count; i++)
{
if (this.GridViewSearchResult.Columns[i].Visible)
{
sb.Append(@"""" + this.GridViewSearchResult.Columns[i].HeaderText + @"""" + ",");
}
}
sb.Append("\n");
foreach (DataRow row in ds.Tables[0].Rows)
{
for (int j = 0; j < this.GridViewSearchResult.Columns.Count;j++ )
{
sb.Append(@""""+row[this.GridViewSearchResult.Columns[j].HeaderText ].ToString().Replace(@"""",@"""""")+ @"""" + ",");
}
sb.Append("\n");
}
sb.Remove(sb.Length - 2, 1);
//save CSV to client
this.Response.Clear();
this.Response.ContentEncoding = System.Text.Encoding.Default;
this.Response.ContentType = "test/csv";
this.Response.AppendHeader("content-disposition", "attachment; filename=" + string.Format("{0:yyyyMMddhhmmss}", DateTime.Now) + ".csv");
this.Response.Write(sb);
this.Response.End();
导出Excel和CSV格式的最大区别就是
Excel
//取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
csv
//取得数据表各列标题,各标题之间以","分割,最后一个列标题后加回车符
浙公网安备 33010602011771号