关于ASP.NET中将Grid导出到EXCEL乱码的问题
2006-04-15 09:25
633 查看
一般常用的方法为:
DataGrid1.DataSource = this.GetDataSource();
DataGrid1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "gb2312";
EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid1.RenderControl(hw );
Response.Write(tw.ToString());
Response.End();
但是有时导出会出现乱码,有时则不会出现乱码.真是百思不得其解.
我们可以这样解决将
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "gb2312";
换成:
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");
Response.AppendHeader("content-disposition","attachment;filename=\"" + HttpUtility.UrlEncode("全部销售记录["+DateTime.Now.ToString("yyyy-MM-dd")+"]",System.Text.Encoding.UTF8) + ".xls\"");
为什么直接输出到Excel会出现乱码,而用Excel打开这段Html不会出现乱码呢?暂时能解决问题但还是不明白,希望高手能给予释疑!
DataGrid1.DataSource = this.GetDataSource();
DataGrid1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "gb2312";
EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid1.RenderControl(hw );
Response.Write(tw.ToString());
Response.End();
但是有时导出会出现乱码,有时则不会出现乱码.真是百思不得其解.
我们可以这样解决将
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "gb2312";
换成:
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");
Response.AppendHeader("content-disposition","attachment;filename=\"" + HttpUtility.UrlEncode("全部销售记录["+DateTime.Now.ToString("yyyy-MM-dd")+"]",System.Text.Encoding.UTF8) + ".xls\"");
为什么直接输出到Excel会出现乱码,而用Excel打开这段Html不会出现乱码呢?暂时能解决问题但还是不明白,希望高手能给予释疑!
相关文章推荐
- asp.net导出excel乱码问题
- asp.net中将gridview数据导出成Excel问题解决
- Asp.net Gridview 导出到Excel【解决编码乱码问题】
- 关于asp.net导出excel的问题(1)
- ASP.NET 导出到EXCEL LINQ适用 自定义列 自定义数据源 解决中文乱码问题 解决方案
- asp.net 导出excel 中的乱码问题及 到处内容丢失开头的0 的解决方法
- 关于ASP.NET中Request.QueryString的乱码问题(转)
- 关于ASP.NET中Request.QueryString的乱码问题(转载)
- 关于ASP.NET 将数据导出成Excel 的总结[补充]
- Asp.NET导出Excel文件乱码 终极解决方法
- 关于在ASP.NET中以DCOM方式操作Excel的几个问题
- 关于asp.net导出excel的注意(4)
- asp.net 导出excel 问题 (服务器的部署)
- asp.net中从数据库导出为excel问题
- 关于ASP.NET 将数据导出成Excel 的总结[下]
- 在ASP.NET中将GridView数据导出到Word、Excel
- Javascript与ASP.NET(一般处理程序)关于URL(get方式)传递中文乱码问题的解决方案
- asp.net导出excel 问题及服务器的部署dcom组件配置
- asp.net中使用excel类导出Excel文件,并导出到web客户端中遇到的问题
- asp.net导出excel 问题及服务器的部署