婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > DataTable數(shù)據(jù)導出成Excel文件的小例子

DataTable數(shù)據(jù)導出成Excel文件的小例子

熱門標簽:百度地圖標注點擊事件 泰州手機外呼系統(tǒng)軟件 山東防封電銷卡辦理套餐 濟源人工智能電話機器人價格 廈門四川外呼系統(tǒng) 內(nèi)蒙古智能電銷機器人哪家強 杭州智能電話機器人 怎樣在地圖標注消火栓圖形 地圖標注位置多的錢

復制代碼 代碼如下:

///
/// 將DataTable中的數(shù)據(jù)導出到指定的Excel文件中
///
/// Web頁面對象
/// 包含被導出數(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 = https://www.jb51.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 = https://www.jb51.net/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;
}
}

標簽:朔州 百色 新鄉(xiāng) 朝陽 周口 喀什 洛陽 臺州

巨人網(wǎng)絡通訊聲明:本文標題《DataTable數(shù)據(jù)導出成Excel文件的小例子》,本文關鍵詞  DataTable,數(shù)據(jù),導出,成,Excel,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《DataTable數(shù)據(jù)導出成Excel文件的小例子》相關的同類信息!
  • 本頁收集關于DataTable數(shù)據(jù)導出成Excel文件的小例子的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 静海县| 平定县| 克东县| 武功县| 沛县| 寻乌县| 甘洛县| 峡江县| 德江县| 前郭尔| 贺兰县| 济源市| 通州区| 盘锦市| 南汇区| 祁阳县| 鄯善县| 泊头市| 新闻| 含山县| 富顺县| 吴旗县| 柞水县| 纳雍县| 敖汉旗| 萨迦县| 任丘市| 钟祥市| 饶平县| 出国| 菏泽市| 温宿县| 县级市| 盐津县| 喀喇| 大厂| 武定县| 连南| 治多县| 井研县| 汉寿县|