导出Excel文档 解决导出Excel文档显示乱码 在C#桌面程序导出Excel文档
2008-04-10 03:15
435 查看
//以下代码在Win程序运行功能成功,注意的一点是在Win程序中用了ASP.NET的WEB控件DataGrid1
//代码亦可在WEB中运行
//绑定数据到DataGrid1
this.DataGrid1.DataSource = SourceTb.DefaultView;
this.DataGrid1.DataBind();
//将DataGrid1构成的html代码写进StringWriter +
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
string HtmlInfo = tw.ToString().Trim();
//此句可解决导出的Excel文档显示乱码"<meta http-equiv=Content-Type content=text/html;charset=gb2312>"
HtmlInfo="<meta http-equiv=Content-Type content=text/html;charset=gb2312>"+HtmlInfo;
//MessageBox.Show(HtmlInfo,"提示");
//得到Excel文件的物理地址
string FilePathName = "要保存文档的路径//文档名.xls";
System.IO.File.Delete(FilePathName);
FileStream Fs = new FileStream(FilePathName, FileMode.Create);
BinaryWriter BWriter= new BinaryWriter(Fs,System.Text.Encoding.GetEncoding("GB18030"));
//将DataGrid的信息写入Excel文件
BWriter.Write(HtmlInfo);
BWriter.Close();
Fs.Close();
如果没什么特别的要求,第一种可以满足基本导出文档,并不会显示乱码的要求.
其它在WEB上的写法:
一般常用的方法为:
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
this.DataGrid1.DataSource = SourceTb.DefaultView;
this.DataGrid1.DataBind();
//将DataGrid1构成的html代码写进StringWriter
this.DataGrid1.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
this.DataGrid1.RenderControl(hw);
string HtmlInfo = tw.ToString().Trim();
string DocFileName = "审批信息.xls";
string FilePathName = Request.PhysicalPath;
FilePathName = FilePathName.Substring(0,FilePathName.LastIndexOf("//"));
//得到Excel文件的物理地址
FilePathName = FilePathName +"//" + DocFileName;
System.IO.File.Delete(FilePathName);
FileStream Fs = new FileStream(FilePathName, FileMode.Create);
BinaryWriter BWriter= new BinaryWriter(Fs,System.Text.Encoding.GetEncoding("GB18030"));
//将DataGrid的信息写入Excel文件
BWriter.Write(HtmlInfo);
BWriter.Close();
Fs.Close();
//代码亦可在WEB中运行
//绑定数据到DataGrid1
this.DataGrid1.DataSource = SourceTb.DefaultView;
this.DataGrid1.DataBind();
//将DataGrid1构成的html代码写进StringWriter +
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
string HtmlInfo = tw.ToString().Trim();
//此句可解决导出的Excel文档显示乱码"<meta http-equiv=Content-Type content=text/html;charset=gb2312>"
HtmlInfo="<meta http-equiv=Content-Type content=text/html;charset=gb2312>"+HtmlInfo;
//MessageBox.Show(HtmlInfo,"提示");
//得到Excel文件的物理地址
string FilePathName = "要保存文档的路径//文档名.xls";
System.IO.File.Delete(FilePathName);
FileStream Fs = new FileStream(FilePathName, FileMode.Create);
BinaryWriter BWriter= new BinaryWriter(Fs,System.Text.Encoding.GetEncoding("GB18030"));
//将DataGrid的信息写入Excel文件
BWriter.Write(HtmlInfo);
BWriter.Close();
Fs.Close();
如果没什么特别的要求,第一种可以满足基本导出文档,并不会显示乱码的要求.
其它在WEB上的写法:
一般常用的方法为:
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
this.DataGrid1.DataSource = SourceTb.DefaultView;
this.DataGrid1.DataBind();
//将DataGrid1构成的html代码写进StringWriter
this.DataGrid1.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
this.DataGrid1.RenderControl(hw);
string HtmlInfo = tw.ToString().Trim();
string DocFileName = "审批信息.xls";
string FilePathName = Request.PhysicalPath;
FilePathName = FilePathName.Substring(0,FilePathName.LastIndexOf("//"));
//得到Excel文件的物理地址
FilePathName = FilePathName +"//" + DocFileName;
System.IO.File.Delete(FilePathName);
FileStream Fs = new FileStream(FilePathName, FileMode.Create);
BinaryWriter BWriter= new BinaryWriter(Fs,System.Text.Encoding.GetEncoding("GB18030"));
//将DataGrid的信息写入Excel文件
BWriter.Write(HtmlInfo);
BWriter.Close();
Fs.Close();
相关文章推荐
- C# DataTable导出EXCEL后身份证等信息显示乱码解决
- 解决C#中txt文档导入数据库时,中文显示乱码的问题
- C# GridView导出excel,字段值前边带0的,导出后不显示0的解决方法
- struts2整合poi导出excel文件名乱码或不显示解决办法
- phpExcel输出xls文档在LINUX下显示乱码的解决办法
- 解决C#中txt文档导入数据库时,中文显示乱码的问题
- C#导出EXCEL显示乱码的问题
- phpExcel输出xls文档显示乱码的解决办法
- c# 导出excel 数字太长而显示为科学计数法的解决方法
- C# 导出 Excel 2003 打开乱码解决办法 希望能帮到大家
- asp.net导出Excel显示中文乱码的解决方法
- asp.net导出Excel显示中文乱码的解决方法
- 用jsp动态输出excel文档和中文乱码问题的解决
- js把数据导出成excel的中文乱码问题解决
- C#中导出excel文档
- php导出excel(xls或xlsx)(解决长数字显示问题)
- C# 导出 Excel 数字列出现‘0’的解决办法
- VIM显示utf-8文档乱码解决方法
- 解决用C#写的窗体应用程序向数据库SQL Server表格中写入中文乱码显示???的方法
- 文件、导出-在Mac系统下Excel转csv文件中文乱码问题解决-by小雨