日期:2014-05-18  浏览次数:20433 次

把GridView中的内容导入Excle中文显示乱码,用 BinaryWriter 的方法写入
代码如下

StringWriter   sw   =   new   StringWriter();
                HtmlTextWriter   htw   =   new   HtmlTextWriter(sw);
                this.GridView1.RenderControl(htw);
                string   strHtml   =   sw.ToString().Trim();

                string   ExcelFileName   =   Session[ "user_code "]+ "temp.xls ";
                string   FilePhysicialPathName   =   Request.PhysicalApplicationPath;

                //生成的Excel文件名
                string   objectExcelFileName   =   Path.Combine(FilePhysicialPathName,   ExcelFileName);

                if   (File.Exists(objectExcelFileName))
                {
                        File.Delete(objectExcelFileName);
                }
                FileStream   fs   =   new   FileStream(objectExcelFileName,   FileMode.Create);
                BinaryWriter   bw   =   new   BinaryWriter(fs,   Encoding.GetEncoding( "GBK "));
                bw.Write(strHtml);
                Response.Redirect( "../ "+Session[ "user_code "]+ "temp.xls ");
                bw.Close();
                fs.Close();
把Encoding.GetEncoding( "GBK ")设置成utf-8或者GB2312都不行,请用过此方法生成Excel的朋友赐教。

------解决方案--------------------
参考一下这:
http://dotnet.aspx.cc/article/700bd3fa-a17f-41dc-b258-0dc572625700/read.aspx
------解决方案--------------------
StreamWriter fs = new StreamWriter (objectExcelFileName, false,Encoding.GetEncoding( "GB2312 ")););
fs.Write(strHtml);
fs.Close();
------解决方案--------------------
StreamWriter fs = new StreamWriter (objectExcelFileName, false,Encoding.UTF7);
fs.Write(strHtml);
fs.Close();
为何是UTF7呢?

我也想知道,为什么UTF8就不行,要UTF7就正常了,以前做项目遇到这个问题,现在也还没明白
------解决方案--------------------
http://www.aspnet168.com.cn/
或许能找到你感兴趣的问题