DataTable數(shù)據(jù)導(dǎo)出成Excel文件的小例子
///
/// 將DataTable中的數(shù)據(jù)導(dǎo)出到指定的Excel文件中
///
/// Web頁(yè)面對(duì)象
/// 包含被導(dǎo)出數(shù)據(jù)的DataTable對(duì)象
/// Excel文件的名稱
public static void Export(System.Web.UI.Page page,System.Data.DataTable tab,string FileName)
{
System.Web.HttpResponse httpResponse = page.Response;
System.Web.UI.WebControls.DataGrid dataGrid=new System.Web.UI.WebControls.DataGrid();
dataGrid.DataSource=tab.DefaultView;
dataGrid.AllowPaging = false;
dataGrid.HeaderStyle.BackColor = System.Drawing.Color.Green;
dataGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
dataGrid.HeaderStyle.Font.Bold = true;
dataGrid.DataBind();
httpResponse.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8)); //filename="*.xls";
httpResponse.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
httpResponse.ContentType ="application/ms-excel";
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
dataGrid.RenderControl(hw);
string filePath = page.Server.MapPath("..")+"http://Files//" +FileName;
System.IO.StreamWriter sw = System.IO.File.CreateText(filePath);
sw.Write(tw.ToString());
sw.Close();
DownFile(httpResponse,FileName,filePath);
httpResponse.End();
}
private static 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同時(shí)下載數(shù)據(jù)
byte[] readData = http://m.fzitv.net/yongle_tianya/archive/2011/10/24/new byte[size];//指定緩沖區(qū)的大小
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 = http://m.fzitv.net/yongle_tianya/archive/2011/10/24/new byte[size];
isEnd=true;
}
fs.Read(readData, 0, size);//讀入一個(gè)壓縮塊
Response.BinaryWrite(readData);
fPos+=size;
}
fs.Close();
System.IO.File.Delete(fullPath);
return true;
}
catch
{
return false;
}
}
相關(guān)文章
asp.net 數(shù)據(jù)綁定時(shí)對(duì)數(shù)據(jù)列做個(gè)性化處理
asp.net 數(shù)據(jù)綁定時(shí)對(duì)數(shù)據(jù)列做個(gè)性化處理,需要的朋友可以參考下。2011-12-12
ASP.NET?使用?Dispose?釋放資源的四種方法詳細(xì)介紹
本篇文章主要介紹了ASP.NET?使用?Dispose?釋放資源的四種方法,有興趣的同學(xué)可以來(lái)看看,喜歡的話記得收藏一下哦,方便下次瀏覽觀看2021-11-11
.Net?7.0實(shí)現(xiàn)支付寶退款和結(jié)果查詢接口
支付寶對(duì) .Net 的支持還是比較充分的,在每個(gè)接口文檔中都有關(guān)于 C# 語(yǔ)言的示例,這樣就大大降低了對(duì)接的難度,很容易上手,這篇文章主要介紹了支付寶退款和結(jié)果查詢接口簡(jiǎn)單實(shí)現(xiàn)(.Net?7.0),需要的朋友可以參考下2024-07-07
asp.net利用NamingContainer屬性獲取GridView行號(hào)的方法
在最近的一個(gè)項(xiàng)目中,用到在GridView模板列中添加有DropDownList控件,并開啟其AutoPostback屬性。當(dāng)發(fā)生SelectedIndexChanged事件時(shí),想同時(shí)獲取其所在的行號(hào),從而獲取相應(yīng)的行信息。2013-07-07
.net生成縮略圖及水印圖片時(shí)出現(xiàn)GDI+中發(fā)生一般性錯(cuò)誤解決方法
這篇文章主要介紹了.net生成縮略圖及水印圖片時(shí)出現(xiàn)GDI+中發(fā)生一般性錯(cuò)誤解決方法 ,需要的朋友可以參考下2014-11-11
asp.net利用cookie保存用戶密碼實(shí)現(xiàn)自動(dòng)登錄的方法
這篇文章主要介紹了asp.net利用cookie保存用戶密碼實(shí)現(xiàn)自動(dòng)登錄的方法,實(shí)例分析了asp.net針對(duì)cookie的創(chuàng)建、提取與銷毀操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01
Visual Studio 2013更新內(nèi)容簡(jiǎn)介
這篇文章主要為大家分享了Visual Studio 2013更新內(nèi)容簡(jiǎn)介,感興趣的小伙伴們可以參考一下2016-05-05

