请教高手如何解决ASP.NET汇出资料到Excel中出现乱码的问题,來者有分!!!! 在线等待

duyl 2002-12-30 10:35:49

我使用的代码为
public void ExportWord(System.Web.UI.Page thisPage)
{
thisPage.Response.AppendHeader("Content-Disposition","attachment; filename=Word.doc"); //filename=Report.xls
thisPage.Response.Charset = "UTF-8";
thisPage.Response.ContentType="application/msword";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel

thisPage.Page.EnableViewState = false; //Turn off the view state.

System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

thisPage.RenderControl(hw); //Get the HTML for the control.
thisPage.Response.Write(tw.ToString()); //Write the HTML back to the browser.
thisPage.Response.End();
}

因为我们要实现多国语言,在英文语系下Datagrid中的资料有中文的地方就是乱码,并且当在中文语系时,若Datagrid中的资料不多,可以正常显示中文,只要资料较多时中文就又是乱码!很困惑,速盼高手解答
另:我将汇出成的doc檔用Web方式预览,它默认的charset=windows-1252,将其变为utf-8时就可正常显示中文, excel与doc一样
...全文
49 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
srxgs 2003-01-05
  • 打赏
  • 举报
回复
不知你解决没有,这是我的代码,测试通过!!

Response.AddHeader("Content-Disposition ", "attachment;")
Response.ContentType = "application/vnd.ms-excel"
Me.EnableViewState = False
Response.Charset = ""
Response.ContentEncoding = System.Text.Encoding.UTF8
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
Datagrid1.Visible = True
Datagrid1.RenderControl(hw)

Response.Write(tw.ToString())
Response.End()
duyl 2003-01-02
  • 打赏
  • 举报
回复
我把以前所有關於這方面的帖子都翻出來了,這個問題已經是老問題了,但一直都沒有解決!請各位大俠幫忙呀
duyl 2002-12-31
  • 打赏
  • 举报
回复
To: Jouzen()

謝謝
可否能結合代碼說的更詳細些,這個問題困擾了好久,請關註
Jouzen 2002-12-31
  • 打赏
  • 举报
回复
在读取写入的地方需要设置编码 System.Text.Encoding.Default
Lancer903 2002-12-31
  • 打赏
  • 举报
回复
关注ing
duyl 2002-12-31
  • 打赏
  • 举报
回复
To: tigerAnn(拼了)

謝謝
我照你說的剛試過了,還是不行,請關註
鸟菜老 2002-12-30
  • 打赏
  • 举报
回复
//试试这样写,这句代码的位置要对,写在其它地方的话,好像有问题!
thisPage.Response.ContentEncoding = System.Text.Encoding.UTF8;
thisPage.Page.EnableViewState = false;
duyl 2002-12-30
  • 打赏
  • 举报
回复
To: chengwenjian(程文建)

謝謝!
這種方法我看到過,可是我使用的是System.IO.StringWriter方法,並不是用的
FileStream方法,所以不知道在那裡強制指定System.Text.Encoding.Unicode
請關註
chengwenjian 2002-12-30
  • 打赏
  • 举报
回复
objFileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
objStreamWriter = new StreamWriter(objFileStream,System.Text.Encoding.Unicode);
=================
注意,就是System.Text.Encoding.Unicode在起作用。

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧