国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net與excel互操作實現代碼

2024-07-10 12:42:26
字體:
來源:轉載
供稿:網友
代碼如下:
/// <summary>
/// 將datatable中的數據導出到指定的excel文件中
/// </summary>
/// <param name="page">web頁面對象</param>
/// <param name="tab">包含被導出數據的datatable對象</param>
/// <param name="filename">excel文件的名稱</param>
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同時下載數據
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;
}
}

/// <summary>
/// 將指定excel文件中的數據轉換成datatable對象,供應用程序進一步處理
/// </summary>
/// <param name="filepath"></param>
/// <returns></returns>
public static system.data.datatable import(string filepath)
{
system.data.datatable rs = new system.data.datatable();
bool canopen=false;

oledbconnection conn = new oledbconnection("provider=microsoft.jet.oledb.4.0;"+
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 澄江县| 丰城市| 来宾市| 密云县| 金坛市| 彝良县| 涡阳县| 扎兰屯市| 神农架林区| 太仆寺旗| 张家口市| 屯昌县| 盈江县| 横山县| 九龙县| 洞口县| 白山市| 满洲里市| 吴堡县| 西乌| 潢川县| 永昌县| 新和县| 奈曼旗| 安远县| 莆田市| 阳谷县| 砀山县| 浦江县| 嵊州市| 正镶白旗| 错那县| 苍溪县| 阜南县| 伊宁市| 西平县| 叙永县| 长寿区| 昌平区| 永平县| 吉水县|