DataGrid数据导出乱码解决方法

之前做bs项目时,对于datagrid的数据导出都是直接调用这个方法:
/// <summary>
        
/// 将DataGrid显示的数据导出成Excel
        
/// </summary>
        
/// <param name="DG">DataGrid</param>
        
/// <param name="objPage"></param>

        public static void DGToExcel( DataGrid DG ,Page objPage)
        
{
            objPage.Response.Clear(); 
            objPage.Response.Buffer
= true
            objPage.Response.Charset
="GB2312";
            objPage.Response.AppendHeader(
"Content-Disposition","attachment;filename=FileName.xls"); 
            objPage.Response.ContentEncoding
=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
            objPage.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
            objPage.EnableViewState = false;    
            System.Globalization.CultureInfo myCItrad 
= new System.Globalization.CultureInfo("ZH-CN",true);
            System.IO.StringWriter oStringWriter 
= new System.IO.StringWriter(myCItrad); 
            System.Web.UI.HtmlTextWriter oHtmlTextWriter 
= new System.Web.UI.HtmlTextWriter(oStringWriter);
            DG.RenderControl(oHtmlTextWriter); 
            objPage.Response.Write(oStringWriter.ToString());
            objPage.Response.End();    
        }
一直没有遇到过问题,后来遇到乱码的怪问题,最后终于找到一个前人解决了的方法:
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(dgKC,"FileName.xls","application/ms-excel");//导出至Excel

该方法是直接在服务器端生成Excel文件,然后再写入客户端下载。
posted on 2007-01-16 10:01  冷月孤峰  阅读(1004)  评论(0)    收藏  举报