asp.net導出Excel亂碼的原因及解決方法
更新時間:2014年02月19日 15:41:04 作者:
asp.net導出Excel亂碼的情況時有發(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();
}
關鍵:
復制代碼 代碼如下:
Response.Charset = "UTF-8";//添加編碼格式
Response.ClearContent();
Response.Clear();
Response.ContentEncoding = System.Text.Encoding.UTF8;//表格內容添加編碼格式
Response.HeaderEncoding = System.Text.Encoding.UTF8;//表頭添加編碼格式
上邊如果解決不了還可以用
復制代碼 代碼如下:
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();
關鍵:
復制代碼 代碼如下:
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
注意觀察,主要原因其實就是編碼格式問題。
現(xiàn)在就能防止導出時候亂碼問題了
您可能感興趣的文章:
- 直接在線預覽Word、Excel、TXT文件之ASP.NET
- ASP.NET使用GridView導出Excel實現(xiàn)方法
- asp.net導出excel數(shù)據(jù)的常見方法匯總
- Asp.net導出Excel/Csv文本格式數(shù)據(jù)的方法
- Asp.Net使用Npoi導入導出Excel的方法
- asp.net使用npoi讀取excel模板并導出下載詳解
- ASP.NET導出數(shù)據(jù)到Excel的實現(xiàn)方法
- .Net中導出數(shù)據(jù)到Excel(asp.net和winform程序中)
- asp.net生成Excel并導出下載五種實現(xiàn)方法
- ASP.NET導出Excel打開時提示:與文件擴展名指定文件不一致解決方法
- asp.net中如何批量導出access某表內容到word文檔
- asp.net 按指定模板導出word,pdf實例代碼
- asp.net+Ligerui實現(xiàn)grid導出Excel和Word的方法
相關文章
ASP.NET WebForm中<%=%>與<%#%>的區(qū)別
這篇文章主要介紹了ASP.NET WebForm中<%=%>與<%#%>的區(qū)別,需要的朋友可以參考下2015-01-01
Asp.net下用JQuery找出哪一個元素引起PostBack
在Asp.net webform中,如何找出哪一個按鈕觸發(fā)Button PostBack事件。2010-06-06
c# Random快速連續(xù)產(chǎn)生相同隨機數(shù)的解決方案
在寫數(shù)獨基類的時候為了產(chǎn)生隨機數(shù)的時候遇到奇怪的問題2009-03-03
.net core如何在網(wǎng)絡高并發(fā)下提高JSON的處理效率詳解
這篇文章主要給大家介紹了關于.net core如何在網(wǎng)絡高并發(fā)下提高JSON的處理效率的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用.net core具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-04-04

