华域联盟 .Net asp.net导出Excel乱码的原因及解决方法

asp.net导出Excel乱码的原因及解决方法

复制代码 代码如下:

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");

注意观察,主要原因其实就是编码格式问题。

现在就能防止导出时候乱码问题了

您可能感兴趣的文章:

  • 直接在线预览Word、Excel、TXT文件之ASP.NET
  • ASP.NET使用GridView导出Excel实现方法
  • asp.net导出excel数据的常见方法汇总
  • Asp.net导出Excel/Csv文本格式数据的方法
  • Asp.Net使用Npoi导入导出Excel的方法
  • asp.net使用npoi读取excel模板并导出下载详解
  • ASP.NET导出数据到Excel的实现方法
  • .Net中导出数据到Excel(asp.net和winform程序中)
  • asp.net生成Excel并导出下载五种实现方法
  • ASP.NET导出Excel打开时提示:与文件扩展名指定文件不一致解决方法
  • asp.net中如何批量导出access某表内容到word文档
  • asp.net 按指定模板导出word,pdf实例代码
  • asp.net+Ligerui实现grid导出Excel和Word的方法

本文由 华域联盟 原创撰写:华域联盟 » asp.net导出Excel乱码的原因及解决方法

转载请保留出处和原文链接:https://www.cnhackhy.com/53843.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部