asp.net導(dǎo)出Excel亂碼的原因及解決方法
更新時間:2014年02月19日 15:41:04 作者:
asp.net導(dǎo)出Excel亂碼的情況時有發(fā)生,本文有個不錯的解決方法,大家可以參考下
復(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");
注意觀察,主要原因其實就是編碼格式問題。
現(xiàn)在就能防止導(dǎo)出時候亂碼問題了
您可能感興趣的文章:
- 直接在線預(yù)覽Word、Excel、TXT文件之ASP.NET
- ASP.NET使用GridView導(dǎo)出Excel實現(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的實現(xiàn)方法
- .Net中導(dǎo)出數(shù)據(jù)到Excel(asp.net和winform程序中)
- asp.net生成Excel并導(dǎo)出下載五種實現(xiàn)方法
- ASP.NET導(dǎo)出Excel打開時提示:與文件擴展名指定文件不一致解決方法
- asp.net中如何批量導(dǎo)出access某表內(nèi)容到word文檔
- asp.net 按指定模板導(dǎo)出word,pdf實例代碼
- asp.net+Ligerui實現(xiàn)grid導(dǎo)出Excel和Word的方法
相關(guān)文章
在?.NET?中使用?FixedTimeEquals?應(yīng)對計時攻擊的例子
在計算機安全中,計時攻擊(Timing attack)是旁道攻擊 (Side-channel attack) 的一種,而旁道攻擊是根據(jù)計算機處理過程發(fā)出的信息進行分析,這篇文章主要介紹了在?.NET?中使用?FixedTimeEquals?應(yīng)對計時攻擊,需要的朋友可以參考下2022-06-06ASP.NET實現(xiàn)從服務(wù)器下載文件問題處理
本文主要介紹了ASP.NET實現(xiàn)從服務(wù)器下載文件問題處理,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02用Fine Uploader+ASP.NET MVC實現(xiàn)ajax文件上傳[代碼示例]
Fine Uploader(http://fineuploader.com/)是一個實現(xiàn) ajax 上傳文件的 Javascript 組件2013-01-01.Net Core中使用ExceptionFilter過濾器的方法
這篇文章主要介紹了.Net Core中使用ExceptionFilter過濾器的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03