GridView导出 Excel文件内容出现乱码?
GridView导出 Excel文件内容出现乱码?导出内容在office excel 2003 中可以打开, 但在office excel 2007 中打开就出现乱码.请大侠们帮助看下!我的.cs后台代码如下:
//导出Excel文件
protected void btnExportExcelFile_Click(object sender, EventArgs e)
{
string FileName = "巡检工单商户信息表";
Response.Clear();
Response.Buffer = true;
Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls\"");
Response.ContentEncoding = System.Text.Encoding.UTF7;
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);
this.gvPatrolAndOrder.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
} --------------------编程问答-------------------- Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentEncoding =System.Text.Encoding.getEnconde("gb2312")试试 --------------------编程问答-------------------- public void DataTable2Excel(System.Data.DataTable dtData, String FileName)
{
System.Web.UI.WebControls.GridView dgExport = null;
//当前对话
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
//IO用于导出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
if (dtData != null)
{
//设置编码和附件格式
System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8);//作用是方式中文文件名乱码
curContext.Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding = System.Text.Encoding.UTF7;
curContext.Response.Charset = "GB2312";
//导出Excel文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
//为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的GridView
dgExport = new System.Web.UI.WebControls.GridView();
dgExport.DataSource = dtData.DefaultView;
dgExport.AllowPaging = false;
dgExport.DataBind();
//下载到客户端
dgExport.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
} --------------------编程问答-------------------- Response.ContentEncoding = System.Text.Encoding.UTF7;
改为
Response.ContentEncoding = System.Text.Encoding.UTF8;
--------------------编程问答-------------------- 另外在HTML代码里需要加上 EnableEventValidation = "false" 否则会出现异常
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" EnableEventValidation = "false" AutoEventWireup="true" CodeFile="PlaneAnalysisShow.aspx.cs" Inherits="OLAP_PlaneAnalysisShow"%>
--------------------编程问答--------------------
Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls\"");
改成
Response.AppendHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.Default) + ".xls\"");
--------------------编程问答-------------------- 这个问题楼主怎么解决的啊,我也遇到这样的问题啊,在office2007中打开是乱码 --------------------编程问答-------------------- 在你点击导出execl时,先保存后再次打开就没乱码了。 --------------------编程问答-------------------- 这一句的问题
Response.ContentEncoding = System.Text.Encoding.UTF7;
设置成UTF8就不会有乱码了 --------------------编程问答-------------------- protected void Button1_Click(object sender, EventArgs e)//这是导出按钮 {
#region 导出到Excel
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AddHeader("content-disposition",
"attachment;filename=MyProjectFileName.xls");
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
G_cli1.AllowPaging = false;
G_cli1.Columns[1].Visible = false;//为什么加这句话呢
这句话的实际意思是第二列不导出 如果不加 导出是乱码 你把你不需要的一列禁了以后试试
为什么禁了一列导出就不乱码了 原因我也不清楚 仅仅是自己实践证明的!!!
Bind();
G_cli1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
G_cli1.AllowPaging = true;
Bind();
#endregion
}
public override void VerifyRenderingInServerForm(Control control)//这也必须加上
{
}
补充:.NET技术 , ASP.NET