C 匯出Excel彙總

2021-04-17 22:16:15 字數 1812 閱讀 4765

一、asp.net中匯出execl的方法:

在asp.net中匯出execl有兩種方法,一種是將匯出的檔案存放在伺服器某個資料夾下面,然後將檔案位址輸出在瀏覽器上;一種是將檔案直接將檔案輸出流寫給瀏覽器。在response輸出時,t分隔的資料,匯出execl時,等價於分列,n等價於換行。

1、將整個html全部輸出execl

此法將html中所有的內容,如按鈕,**,等全部輸出到execl中。

response.clear();   

response.buffer=   true;   

response.contentencoding=system.text.encoding.utf8;  

this.enableviewstate   =   false;  

2、將datagrid控制項中的資料匯出execl

上述方法雖然實現了匯出的功能,但同時把按鈕、分頁框等html中的所有輸出資訊導了進去。而我們一般要匯出的是資料,datagrid控制項上的資料。

system.web.ui.control ctl=this.datagrid1;

//datagrid1是你在窗體中拖放的控制項

如果你的datagrid用了分頁,它匯出的是當前頁的資訊,也就是它匯出的是datagrid中顯示的資訊。而不是你select語句的全部資訊。

為方便使用,寫成方法如下:

public void dgtoexcel(system.web.ui.control ctl)  

用法:dgtoexcel(datagrid1);

3、將dataset中的資料匯出execl

有了上邊的思路,就是將在匯出的資訊,輸出(response)客戶端,這樣就可以匯出了。那麼把dataset中的資料匯出,也就是把dataset中的表中的各行資訊,以ms-excel的格式response到http流,這樣就ok了。說明:引數ds應為填充有資料表的dataset,檔名是全名,包括字尾名,如execl2006.xls

public  void createexcel(dataset ds,string filename)

workbook.close(missing,missing,missing); }

三、附註:

雖然都是實現匯出execl的功能,但在asp.net和winform的程式中,實現的**是各不相同的。在asp.net中,是在伺服器端讀取資料,在伺服器端把資料以ms-execl的格式,以response輸出到瀏覽器(客戶端);而在winform中,是把資料讀到客戶端(因為winform執行端就是客戶端),然後呼叫客戶端安裝的office元件,將讀到的資料寫在execl的工作簿中。

C 讀取Excel,匯出Excel

c 讀取excel 方法一 oledb方式 1 定義連線字串 string strconn provider microsoft.jet.oledb.4.0 data source exce檔案路徑及名字 extended properties excel 8.0 hdr false imex 1 ...

C 讀取Excel,匯出Excel

c 讀取excel 方法一 oledb方式 1 定義連線字串 string strconn provider microsoft.jet.oledb.4.0 data source exce檔案路徑及名字 extended properties excel 8.0 hdr false imex 1 ...

C 匯出Excel 匯出資料到Excel模板檔案裡

using excel microsoft.office.interop.excel using system.reflection missing 開啟模板檔案,得到workbook物件 workbook excel.workbooks.open templatepath,missing,miss...