從DataGrid匯出Excel產生亂碼的一個很好的解決方案

來源:互聯網
上載者:User
什麼也不說了,從網上找來的,
引用自:  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這個函數罷了.

以上方法可以很有效地解決匯出的亂碼問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.