采用UTF8格式导出Excel存在乱码解决方法

摘要:
目前,当我们开发Excel导出时,大多数人都使用ExportToExcelCommon类导出Excel。通常情况下,不会出现乱码,但有时它仍会生成乱码。解决方案很简单。您只需要向ExportToExcel()方法再添加一个代码。ExportToExcel()方法如下:stringfileName;HttpContext.Current.Response(Http上下文当前响应)。缓冲区=真;HttpContext。
目前我们开发Excel导出时,多数使用ExportToExcelCommon类导出Excel,通常情况下不会产生乱码,但在某些时候还是会产生乱码。解决方法很简单,只需要在ExportToExcel()方法中多加一句代码即可,ExportToExcel()方法如下:

stringfileName;
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.UTF8;
            HttpContext.Current.Response.HeaderEncoding =System.Text.Encoding.UTF8;

            fileName = string.Format("AOMS Export-{0:yyyy-MM-dd_HH_mm_ss}.xls", DateTime.Now);
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" +fileName);
            //设置输出文件类型为excel文件。
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            StringWriter tw = newSystem.IO.StringWriter();
            HtmlTextWriter hw = newSystem.Web.UI.HtmlTextWriter(tw);
            gvw.RenderControl(hw);
            if (!string.IsNullOrEmpty(title))
            {
                HttpContext.Current.Response.Write("<b><center><font size=3 face=Verdana color=#0000FF>" + title + "</font></center></b>");
            }
            HttpContext.Current.Response.Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=UTF-8\"/>");
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.Close();
            HttpContext.Current.Response.End();

            gvw.Dispose();
            tw.Dispose();
            hw.Dispose();

            gvw = null;
            tw = null;
         hw = null;
注意上述代码的重点在于这一句:
HttpContext.Current.Response.Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=UTF-8\"/>");
在导出方法中加入这一句代码,即可防止采用UTF-8格式导出Excel时出现乱码。

免责声明:文章转载自《采用UTF8格式导出Excel存在乱码解决方法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇.net破解二(修改dll)Maxwell仿真变压器后处理——变压器参数计算下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C#获取Excel中所有的Sheet名称

原文地址:http://blog.csdn.net/qq1010726055/article/details/6858849 Excel.Application myExcel = newExcel.Application(); object missing =System.Reflection.Missing.Value; myExcel.Appli...

java poi 操作Excel常用方法总结

原文地址:http://lilinhui.iteye.com/blog/1163040 官网地址:http://poi.apache.org/spreadsheet/quick-guide.html 一、 POI简介             Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft O...

jquery动态实现填充下拉框

 当点下拉框时动态加载后台数据。 后台代码 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8...

umi-request 一个新的请求工具

简单封装 npm install umi-request --save 文档,具体封装可以多看看文档配置 import { extend } from "umi-request"; const request = extend({ prefix: "xxx",//相当于baseurl timeout: 10000, errorHandle...

将数据填充到已有的EXECL模板中

导出execl网上一大堆,最近遇到将数据导出到已有的execl模板中,具体到某行列,动态加载数据。 添加Microsoft.Office.Interop.Excel 引用 1 /// <summary> 2 ///DataGridView 导出到execl模板中 3 /// </summary> 4...

python(openpyxl)复制excel数据到另一个excel数据表

之前写过https://www.cnblogs.com/pu369/p/15430224.html 代码: #-*- coding: utf-8 -*- fromopenpyxl import Workbook fromopenpyxl import load_workbook import openpyxl #globalitems =[] file1...