日期:2014-05-17 浏览次数:20537 次
Response.Clear();
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.HeaderEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "text/csv";
Response.BinaryWrite(new byte[] { 0xEF, 0xBB, 0xBF }); //这句是重点哦,不乱码的保证
Response.AddHeader("Content-Disposition", string.Format("attachment;filename=Customer{0:yyyy-MM-dd}.csv;", DateTime.Now));
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();
//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.Appen