參考引用來源:[ASP.NET]GridView轉成CVS檔案 (解決中文字亂碼問題)
--
private void ExportCVS()
{
Response.Clear();
Response.Buffer = true;
string filename = DateTime.Now.ToString("yyyyMMdd") + "test.csv";
Response.AddHeader("content-disposition", "attachment;filename=" + filename);
Response.Charset = "BIG5";
Response.ContentType = "application/text";
Response.ContentEncoding = Encoding.GetEncoding(950);//950就是所謂的BIG5
gvList.AllowPaging = false;
//gvList.DataBind();
QueryData();
StringBuilder sb = new StringBuilder();
for (int k = 0; k < gvList.Columns.Count; k++)
{
//add separator
sb.Append(gvList.Columns[k].HeaderText + ',');
}
//append new line
sb.Append("\r\n");
for (int i = 0; i < gvList.Rows.Count; i++)
{
for (int k = 0; k < gvList.Columns.Count; k++)
{
//add separator
sb.Append(gvList.Rows[i].Cells[k].Text + ',');
}
//append new line
sb.Append("\r\n");
}
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
gvList.AllowPaging = true ;
QueryData();
}
沒有留言:
張貼留言