惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
M
MIT News - Artificial intelligence
D
Docker
量子位
T
Tailwind CSS Blog
人人都是产品经理
人人都是产品经理
月光博客
月光博客
有赞技术团队
有赞技术团队
J
Java Code Geeks
A
About on SuperTechFans
P
Proofpoint News Feed
Jina AI
Jina AI
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
V
V2EX
GbyAI
GbyAI
F
Fortinet All Blogs

博客园 - 踏歌长行

前端知识质量内容网址 NuGet 质量博客链接 Entity Framework 质量博客链接 LINK1123:failure during conversion to COFF:file invalid or corrupt 未授权用户在此计算机上的请求登录类型 System.ServiceModel.AddressAccessDeniedException 80070005 Access is denied in Windows 2008 How to install ASP.NET 1.1 with IIS7 on Vista and Windows 2008 Unable to find script library '/aspnet_client/system-web/1-1-4322/webvalidation.js' JQuery file upload Access is denied in IE 7, 8, 9 ReportViewer 自适应高度 单元测试之模拟Mock 单元测试之 Xunit ASP.NET MVC ajax 提交列表到 Action VS 2010 制作 Windows Service 安装包之用户界面 VS 2010 制作 Windows Service 安装包 Postback 之后保持浏览器滚动条的位置 VS 2010 开发 ActiveX 自动升级外篇 VS 2010 开发 ActiveX 制作 cab 包
DataGrid 导出 Excel 中文乱码
踏歌长行 · 2014-06-05 · via 博客园 - 踏歌长行
public static void DataGridToExcel(DataGrid dataGrid, string fileName)
{
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            // 1. Set ContentEncoding
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
            // 2. Set harset
            HttpContext.Current.Response.Charset = "utf-8";
            HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            
            var sw = new StringWriter();
            var htmlw = new HtmlTextWriter(sw);

            dataGrid.RenderControl(htmlw);
            // 3. Set meta data
            HttpContext.Current.Response.Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=utf-8\"/>");
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();
}