DataTable數(shù)據(jù)導(dǎo)出成Excel文件的小例子_.Net教程
推薦:ASP.NET筆記之 Repeater的使用本篇文章小編為大家介紹,ASP.NET筆記之 Repeater的使用。需要的朋友參考下
///
/// 將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://www.hl5o.cn/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://www.hl5o.cn/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;
}
}
分享:.net 中的SqlConnection連接池機(jī)制詳解.net 中通過(guò) SqlConnection 連接 sql server,我們會(huì)發(fā)現(xiàn)第一次連接時(shí)總是很耗時(shí),但后面連接就很快,這個(gè)其實(shí)和SqlConnection 的連接池機(jī)制有關(guān)
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發(fā)送Email實(shí)例(可帶附件)
- js實(shí)現(xiàn)廣告漂浮效果的小例子
- asp.net Repeater 數(shù)據(jù)綁定的具體實(shí)現(xiàn)
- Asp.Net 無(wú)刷新文件上傳并顯示進(jìn)度條的實(shí)現(xiàn)方法及思路
- Asp.net獲取客戶端IP常見(jiàn)代碼存在的偽造IP問(wèn)題探討
- VS2010 水晶報(bào)表的使用方法
- ASP.NET中操作SQL數(shù)據(jù)庫(kù)(連接字符串的配置及獲取)
- asp.net頁(yè)面?zhèn)髦禍y(cè)試實(shí)例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲(chǔ)過(guò)程實(shí)現(xiàn)分頁(yè)示例代碼
.Net教程Rss訂閱編程教程搜索
.Net教程推薦
- 淺析C#中的Adapter設(shè)計(jì)模式
- 解讀ASP.NET開(kāi)發(fā)的編程習(xí)慣
- asp.net的GridView控件使用方法大全
- ASP.NET蔚昜璃唗蹈趙傖Binary揣湔祫DB or File
- 解析防網(wǎng)站登陸被破解的簡(jiǎn)單方法
- ASP.NET對(duì)IIS中的虛擬目錄進(jìn)行操作
- 點(diǎn)擊提交按鈕后DropDownList的值變?yōu)槟J(rèn)值實(shí)現(xiàn)分析
- Flex與.NET互操作:基于WebService的數(shù)據(jù)訪問(wèn)
- 談ASP.NET創(chuàng)建Web服務(wù)的使用事務(wù)
- .NET下為百度文本編輯器UEditor增加圖片刪除功能示例
- 相關(guān)鏈接:
- 教程說(shuō)明:
.Net教程-DataTable數(shù)據(jù)導(dǎo)出成Excel文件的小例子
。