DataGrid匯出Excel產生亂碼解決方案(轉)
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;
}
}[@more@]
...{
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;
}
}[@more@]
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10172717/viewspace-926877/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 乾貨:blob匯出檔案亂碼解決方案
- 小程式批次匯入excel資料,雲開發資料庫匯出cvs亂碼解決方案Excel資料庫
- EXCEL大資料量匯出的解決方案Excel大資料
- 解決Java POI 匯出Excel時檔名中文亂碼,相容瀏覽器JavaExcel瀏覽器
- mysql 插入中文亂碼解決方案 轉MySql
- sqlldr 匯入中文出現亂碼解決方法SQL
- C#中DataGrid匯出Excel檔案C#Excel
- ORACLE亂碼解決方案Oracle
- Java亂碼解決方案Java
- photoshop匯出png發生未知錯誤的解決方案,ps匯出發生未知錯誤怎麼解決
- 解決Excel資料匯入sqlite中的中文亂碼問題ExcelSQLite
- oracle 輸出中文亂碼問題解決方案Oracle
- mysql匯入匯出資料中文亂碼解決方法小結MySql
- vi/vim配置篇:亂碼產生的原因及解決
- GlobalFoundries生產eMRAM解決方案
- 解決PHP匯出CSV檔案中文亂碼問題PHP
- wpf 視窗程式下將datagrid匯出為excelExcel
- linux Typora修改Windows檔案產生亂碼(未解決)LinuxWindows
- Excel開啟CSV檔案出現亂碼怎麼辦?Excel開啟csv檔案出現亂碼的解決方法Excel
- 解決Mysql匯入亂碼問題MySql
- c#匯出檔案,檔名中文亂碼解決方法C#
- PL/SQL亂碼解決方案(日文)SQL
- 表單傳值出現亂碼問題解決方案
- Excel開啟csv檔案出現亂碼的解決辦法Excel
- Oracle Sql Loader中文字元匯入亂碼的解決方案OracleSQL字元
- oracle中匯入dmp字元亂碼分析和解決方案Oracle字元
- 轉java操作excel匯入匯出JavaExcel
- DataGrid 匯出到 Excel 的幫助類Excel
- ajax讀取txt文字內容出現亂碼解決方案
- Oracle 生僻字亂碼解決方案Oracle
- Discuz門戶模板亂碼解決方案
- 通用模板解決方案,提升影片生產效率
- office for Mac Excel匯入csv檔案亂碼MacExcel
- MySQL直接匯出CSV檔案,並解決中文亂碼的問題MySql
- 電動車生產製造行業的數字化轉型密碼:SAP解決方案行業密碼
- mysql 匯入中文亂碼解決一例MySql
- JavaWeb 亂碼問題終極解決方案!JavaWeb
- MySQL字符集亂碼與解決方案MySql