DataGrid匯出Excel產生亂碼解決方案(轉)

heying1229發表於2007-07-17
private void Export(System.Web.UI.WebControls.DataGrid dg,string fileName,string typeName)
DataGrid匯出Excel產生亂碼解決方案(轉)DataGrid匯出Excel產生亂碼解決方案(轉)
{
DataGrid匯出Excel產生亂碼解決方案(轉) System.Web.HttpResponse httpResponse
= Page.Response;
DataGrid匯出Excel產生亂碼解決方案(轉) httpResponse.AppendHeader
DataGrid匯出Excel產生亂碼解決方案(轉) (
"Content-Disposition","attachment;filename="
DataGrid匯出Excel產生亂碼解決方案(轉)
+HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8));
DataGrid匯出Excel產生亂碼解決方案(轉) httpResponse.ContentEncoding
=System.Text.Encoding.GetEncoding("GB2312");
DataGrid匯出Excel產生亂碼解決方案(轉) httpResponse.ContentType
= typeName;
DataGrid匯出Excel產生亂碼解決方案(轉) System.IO.StringWriter tw
= new System.IO.StringWriter() ;
DataGrid匯出Excel產生亂碼解決方案(轉) System.Web.UI.HtmlTextWriter hw
= new System.Web.UI.HtmlTextWriter (tw);
DataGrid匯出Excel產生亂碼解決方案(轉) dg.RenderControl(hw);
DataGrid匯出Excel產生亂碼解決方案(轉)
string filePath = Server.MapPath("..")+fileName;
DataGrid匯出Excel產生亂碼解決方案(轉) System.IO.StreamWriter sw
= System.IO.File.CreateText(filePath);
DataGrid匯出Excel產生亂碼解決方案(轉) sw.Write(tw.ToString());
DataGrid匯出Excel產生亂碼解決方案(轉) sw.Close();
DataGrid匯出Excel產生亂碼解決方案(轉)
DataGrid匯出Excel產生亂碼解決方案(轉) DownFile(httpResponse,fileName,filePath);
DataGrid匯出Excel產生亂碼解決方案(轉) httpResponse.End();
DataGrid匯出Excel產生亂碼解決方案(轉) }

DataGrid匯出Excel產生亂碼解決方案(轉)
DataGrid匯出Excel產生亂碼解決方案(轉)
private bool DownFile(System.Web.HttpResponse Response,string fileName,string fullPath)
DataGrid匯出Excel產生亂碼解決方案(轉)DataGrid匯出Excel產生亂碼解決方案(轉)
{
DataGrid匯出Excel產生亂碼解決方案(轉)
try
DataGrid匯出Excel產生亂碼解決方案(轉)DataGrid匯出Excel產生亂碼解決方案(轉)
{
DataGrid匯出Excel產生亂碼解決方案(轉) Response.ContentType
= "application/octet-stream";
DataGrid匯出Excel產生亂碼解決方案(轉)
DataGrid匯出Excel產生亂碼解決方案(轉) Response.AppendHeader(
"Content-Disposition","attachment;filename=" +
DataGrid匯出Excel產生亂碼解決方案(轉) HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)
+ ";charset=GB2312");
DataGrid匯出Excel產生亂碼解決方案(轉) System.IO.FileStream fs
= System.IO.File.OpenRead(fullPath);
DataGrid匯出Excel產生亂碼解決方案(轉)
long fLen=fs.Length;
DataGrid匯出Excel產生亂碼解決方案(轉)
int size=102400;//每100K同時下載資料
DataGrid匯出Excel產生亂碼解決方案(轉)
byte[] readData = new byte[size];//指定緩衝區的大小
DataGrid匯出Excel產生亂碼解決方案(轉)
if(size>fLen)size=Convert.ToInt32(fLen);
DataGrid匯出Excel產生亂碼解決方案(轉)
long fPos=0;
DataGrid匯出Excel產生亂碼解決方案(轉)
bool isEnd=false;
DataGrid匯出Excel產生亂碼解決方案(轉)
while (!isEnd)
DataGrid匯出Excel產生亂碼解決方案(轉)DataGrid匯出Excel產生亂碼解決方案(轉)
{
DataGrid匯出Excel產生亂碼解決方案(轉)
if((fPos+size)>fLen)
DataGrid匯出Excel產生亂碼解決方案(轉)DataGrid匯出Excel產生亂碼解決方案(轉)
{
DataGrid匯出Excel產生亂碼解決方案(轉) size
=Convert.ToInt32(fLen-fPos);
DataGrid匯出Excel產生亂碼解決方案(轉) readData
= new byte[size];
DataGrid匯出Excel產生亂碼解決方案(轉) isEnd
=true;
DataGrid匯出Excel產生亂碼解決方案(轉) }

DataGrid匯出Excel產生亂碼解決方案(轉) fs.Read(readData,
0, size);//讀入一個壓縮塊
DataGrid匯出Excel產生亂碼解決方案(轉)
Response.BinaryWrite(readData);
DataGrid匯出Excel產生亂碼解決方案(轉) fPos
+=size;
DataGrid匯出Excel產生亂碼解決方案(轉) }

DataGrid匯出Excel產生亂碼解決方案(轉) fs.Close();
DataGrid匯出Excel產生亂碼解決方案(轉) System.IO.File.Delete(fullPath);
DataGrid匯出Excel產生亂碼解決方案(轉)
return true;
DataGrid匯出Excel產生亂碼解決方案(轉) }

DataGrid匯出Excel產生亂碼解決方案(轉)
catch
DataGrid匯出Excel產生亂碼解決方案(轉)DataGrid匯出Excel產生亂碼解決方案(轉)
{
DataGrid匯出Excel產生亂碼解決方案(轉)
return false;
DataGrid匯出Excel產生亂碼解決方案(轉) }

DataGrid匯出Excel產生亂碼解決方案(轉) }

DataGrid匯出Excel產生亂碼解決方案(轉) }


為了方便大家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;
}
}[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10172717/viewspace-926877/,如需轉載,請註明出處,否則將追究法律責任。

相關文章