导出的EXCEL是乱码,改网页编码无效,大家帮下,谢谢

mistycool 2008-11-25 08:43:46
protected void Button2_Click(object sender, EventArgs e)
{
toexcel(ltPage);
}
protected void toexcel(Control ctl)
{
/*HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=baobiao.xls");
HttpContext.Current.Response.Charset = "UTF8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();*/
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
Response.ContentEncoding = System.Text.Encoding.UTF8;
ctl.RenderControl(oHtmlTextWriter);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
...全文
637 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyingfz 2008-11-25
  • 打赏
  • 举报
回复
试试使用 Myxls 这个开源的组件, 比较好用。
mistycool 2008-11-25
  • 打赏
  • 举报
回复
protected void Button2_Click(object sender, EventArgs e)
{
toexcel(ltPage);
}
protected void toexcel(Control ctl)
{
/*HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=baobiao.xls");
HttpContext.Current.Response.Charset = "UTF8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();*/
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
Response.ContentType = "application/ms-excel;charset=UTF8";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
Response.ContentEncoding = System.Text.Encoding.UTF8;
ctl.RenderControl(oHtmlTextWriter);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
mistycool 2008-11-25
  • 打赏
  • 举报
回复
我改过了,无效的
Zeteyu 2008-11-25
  • 打赏
  • 举报
回复
又或者把Response.Charset = "GB2312";改为Response.Charset = "UTF-8";
Zeteyu 2008-11-25
  • 打赏
  • 举报
回复
protected void Button2_Click(object sender, EventArgs e)
{
toexcel(ltPage);
}

protected void toexcel(Control ctl)
{
/*HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=baobiao.xls");
HttpContext.Current.Response.Charset = "UTF8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();*/
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
Response.ContentType = "application/ms-excel;charset=UTF-8";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
Response.ContentEncoding = System.Text.Encoding.UTF8;
ctl.RenderControl(oHtmlTextWriter);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
}
mistycool 2008-11-25
  • 打赏
  • 举报
回复
up
mistycool 2008-11-25
  • 打赏
  • 举报
回复
up

62,269

社区成员

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

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

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

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