DataTable數(shù)據(jù)導(dǎo)出成Excel文件的小例子
///
/// 將DataTable中的數(shù)據(jù)導(dǎo)出到指定的Excel文件中
///
/// Web頁面對象
/// 包含被導(dǎo)出數(shù)據(jù)的DataTable對象
/// 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ù)據(jù)
byte[] readData = http://www.dbjr.com.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.dbjr.com.cn/yongle_tianya/archive/2011/10/24/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;
}
}
相關(guān)文章
.net?程序通過?crontab?無法啟動手動執(zhí)行腳本啟動的方法
.net 網(wǎng)關(guān)程序需要設(shè)置定時重啟,按照日常操作先把正在運行的 PID kill 掉后,再執(zhí)行啟動服務(wù)。通過腳本無法啟動,試著把 .net 程序?qū)懗煞?wù)后,發(fā)現(xiàn)是可以正常重啟的,本文給大家介紹下.net 程序通過 crontab 無法啟動手動執(zhí)行腳本啟動,感興趣的朋友一起看看吧2021-12-12ASP.NET Core Controller與IOC結(jié)合問題整理
在本篇文章里小編給大家整理了一篇關(guān)于ASP.NET Core Controller與IOC結(jié)合問題整理內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2021-01-01asp.net微信開發(fā)(已關(guān)注用戶管理)
這篇文章主要介紹了asp.net微信開發(fā)中有關(guān)已關(guān)注用戶管理的相關(guān)內(nèi)容,需要的朋友可以參考下2015-11-11ASP.NET?Core獲取正確查詢字符串參數(shù)示例
這篇文章主要為大家介紹了ASP.NET?Core正確獲取查詢字符串參數(shù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05.NET實現(xiàn)ChatGPT的Stream傳輸?shù)倪^程
這篇文章主要介紹了.NET如何實現(xiàn)ChatGPT的Stream傳輸,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07