读书人

DataGrid导出EXCEL中文乱码的有关问题

发布时间: 2012-02-08 19:52:21 作者: rapoo

DataGrid导出EXCEL中文乱码的问题。非诚勿扰!
private void Button1_Click(object sender, System.EventArgs e)
{

string filename ="haha";
ExportData(this.DataGrid1,filename);
}
public void ExportData(DataGrid ds,string attachName)
{
Response.Clear();
Response.Buffer= true;
Response.Charset="utf-8";

Response.AppendHeader("Content-Disposition","attachment;" + attachName + "=.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");
//Response.ContentType指定文件类型 可以为application/ms-excel || application/ms-word || application/ms-txt || application/ms-html || 或其他浏览器可直接支持文档
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";

//关闭 ViewState
EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();//将信息写入字符串
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);//在WEB窗体页上写出一系列连续的HTML特定字符和文本。
//此类提供ASP.NET服务器控件在将HTML内容呈现给客户端时所使用的格式化功能
//获取control的HTML
ds.RenderControl(hw);//将DATAGRID中的内容输出到HtmlTextWriter对象中
// 把HTML写回浏览器
Response.Write(tw.ToString());
Response.End();
}

打开Excel就是乱码,因为是多种语言,不能GBK,应该怎么办?


[解决办法]
Response.Charset="utf-7";

[解决办法]

C# code
Response.AppendHeader("Content-Disposition","attachment;"  +  attachName  +  [color=#FF0000]"=.xls"[/color]); 

读书人网 >asp.net

热点推荐