Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > .NET > C#.NET > 从DataGrid导出Excel产生乱码的一个很好的解决方案
【标  题】:从DataGrid导出Excel产生乱码的一个很好的解决方案
【关键字】:DataGrid,Excel
【来  源】:http://blog.csdn.net/killlkilll/archive/2006/10/16/1336982.aspx

从DataGrid导出Excel产生乱码的一个很好的解决方案

Your Ad Here 什么也不说了,从网上找来的,
引用自:  http://aliketen.cnblogs.com/articles/363650.html

代码如下:
        private void Export(System.Web.UI.WebControls.DataGrid dg,string fileName,string typeName)
        
{
            System.Web.HttpResponse httpResponse 
= Page.Response;
            httpResponse.AppendHeader
                                  (
"Content-Disposition","attachment;filename="
                                   
+HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)); 
            httpResponse.ContentEncoding
=System.Text.Encoding.GetEncoding("GB2312");
            httpResponse.ContentType 
= typeName;
            System.IO.StringWriter  tw 
= new System.IO.StringWriter() ;
            System.Web.UI.HtmlTextWriter hw 
= new System.Web.UI.HtmlTextWriter (tw);
            dg.RenderControl(hw);
            
string filePath = Server.MapPath("..")+fileName;
            System.IO.StreamWriter sw 
= System.IO.File.CreateText(filePath);
            sw.Write(tw.ToString());
            sw.Close();

            DownFile(httpResponse,fileName,filePath);
            httpResponse.End();
        }


        
private  bool DownFile(System.Web.HttpResponse Response,string fileName,string fullPath)
        
{
            
try
            
{
                Response.ContentType 
= "application/octet-stream";

                Response.AppendHeader(
"Content-Disposition","attachment;filename=" + 
                    HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8) 
+ ";charset=GB2312");
                System.IO.FileStream fs
= System.IO.File.OpenRead(fullPath);
                
long fLen=fs.Length;
                
int size=102400;//每100K同时下载数据 
                byte[] readData = new byte[size];//指定缓冲区的大小 
                if(size>fLen)size=Convert.ToInt32(fLen);
                
long fPos=0;
                
bool isEnd=false;
                
while (!isEnd) 
                

                    
if((fPos+size)>fLen)
                    
{
                        size
=Convert.ToInt32(fLen-fPos);
                        readData 
= new byte[size];
                        isEnd
=true;
                    }

                    fs.Read(readData, 
0, size);//读入一个压缩块 
                    Response.BinaryWrite(readData);
                    fPos
+=size;
                }
 
                fs.Close(); 
                System.IO.File.Delete(fullPath);
                
return true;
            }

            
catch
            
{
                
return false;
            }

        }

    }


为了方便大家Copy可以
看如下的代码,保证一样的

private void Export(System.Web.UI.WebControls.DataGrid dg,string fileName,string typeName)
{
System.Web.HttpResponse httpResponse = Page.Response;
httpResponse.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8));
httpResponse.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
httpResponse.ContentType = typeName;
System.IO.StringWriter  tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
dg.RenderControl(hw);
string filePath = Server.MapPath("..")+fileName;
System.IO.StreamWriter sw = System.IO.File.CreateText(filePath);
sw.Write(tw.ToString());
sw.Close();

DownFile(httpResponse,fileName,filePath);
httpResponse.End();
}

private  bool DownFile(System.Web.HttpResponse Response,string fileName,string fullPath)
{
try
{
Response.ContentType = "application/octet-stream";

Response.AppendHeader("Content-Disposition","attachment;filename=" +
HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8) + ";charset=GB2312");
System.IO.FileStream fs= System.IO.File.OpenRead(fullPath);
long fLen=fs.Length;
int size=102400;//每100K同时下载数据
byte[] readData = new byte[size];//指定缓冲区的大小
if(size>fLen)size=Convert.ToInt32(fLen);
long fPos=0;
bool isEnd=false;
while (!isEnd)
{
if((fPos+size)>fLen)
{
size=Convert.ToInt32(fLen-fPos);
readData = new byte[size];
isEnd=true;
}
fs.Read(readData, 0, size);//读入一个压缩块
Response.BinaryWrite(readData);
fPos+=size;
}
fs.Close();
System.IO.File.Delete(fullPath);
return true;
}
catch
{
return false;
}
}


怎么调用?? 其实就是怎么调用Export这个函数罢了.

以上方法可以很有效地解决导出的乱码问题
简易远程遥控程序:【上一篇】
AssemblyInfo.cs文件中的程序集属性:【下一篇】
【相关文章】
  • EXCEL 更新 ACCESS数据库。
  • JAva Excel api 中的编码和中文问题
  • datagrid怎麼自定義表頭
  • 大家给推荐一下从datagrid到出数据到excel的好方法或者是例子。谢谢
  • Visual C# 2005 - 如何将 DataGridView 控件单独截取成一个位图文件
  • Excel 中 vlookup 函数用例
  • ASP.NET Tip: Exporting Data to Excel
  • 自定义DataGrid控件开源
  • JAVA 操作EXCEL
  • 在Asp.net用C#建立动态Excel
  • 【随机文章】
  • 如何用C++进行内存分配
  • Java嵌入式开发之五 谈谈J2ME的几个重要的功能
  • 和一个学生关于数据字典的讨论
  • 禁止PC在出错时发出声音
  • Fedora/Redhat 在线安装更新软件包,yum 篇
  • 关于XGL
  • 断点续传
  • 比较java.io.Externalizable和Serializable
  • oracle学习笔记(一)------oracle基础知识和基本sql语句
  • 快速查找:轻松标注表格中的无效录入数据
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.