復(fù)制代碼 代碼如下:
protected void Excel_Click(object sender, EventArgs e)
{
Response.Charset = "UTF-8";
Response.ClearContent();
Response.Clear();
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.HeaderEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("content-disposition", "attachment; filename=MyExpress.xls");
Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// turn off paging
GridView1.AllowPaging = false;
dataBind();
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
// turn the paging on again
GridView1.AllowPaging = true;
dataBind();
}
關(guān)鍵:
復(fù)制代碼 代碼如下:
Response.Charset = "UTF-8";//添加編碼格式
Response.ClearContent();
Response.Clear();
Response.ContentEncoding = System.Text.Encoding.UTF8;//表格內(nèi)容添加編碼格式
Response.HeaderEncoding = System.Text.Encoding.UTF8;//表頭添加編碼格式
上邊如果解決不了還可以用
復(fù)制代碼 代碼如下:
Response.ClearContent();
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=sumlate.xls");
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
if (GridView2.Rows.Count > 0)
{
GridView2.RenderControl(htw);
}
else
{
GridView1.RenderControl(htw);
}
Response.Write(sw.ToString());
Response.End();
關(guān)鍵:
復(fù)制代碼 代碼如下:
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
注意觀察,主要原因其實(shí)就是編碼格式問題。
現(xiàn)在就能防止導(dǎo)出時候亂碼問題了
您可能感興趣的文章:- 直接在線預(yù)覽Word、Excel、TXT文件之ASP.NET
- ASP.NET使用GridView導(dǎo)出Excel實(shí)現(xiàn)方法
- asp.net導(dǎo)出excel數(shù)據(jù)的常見方法匯總
- Asp.net導(dǎo)出Excel/Csv文本格式數(shù)據(jù)的方法
- Asp.Net使用Npoi導(dǎo)入導(dǎo)出Excel的方法
- asp.net使用npoi讀取excel模板并導(dǎo)出下載詳解
- ASP.NET導(dǎo)出數(shù)據(jù)到Excel的實(shí)現(xiàn)方法
- .Net中導(dǎo)出數(shù)據(jù)到Excel(asp.net和winform程序中)
- asp.net生成Excel并導(dǎo)出下載五種實(shí)現(xiàn)方法
- ASP.NET導(dǎo)出Excel打開時提示:與文件擴(kuò)展名指定文件不一致解決方法
- asp.net中如何批量導(dǎo)出access某表內(nèi)容到word文檔
- asp.net 按指定模板導(dǎo)出word,pdf實(shí)例代碼
- asp.net+Ligerui實(shí)現(xiàn)grid導(dǎo)出Excel和Word的方法