C 使用NPOI匯出Excel

2022-03-03 00:59:00 字數 3656 閱讀 5080

當記錄數超出65536時,有兩種方式處理:

一是呼叫writetodownload65536方法建立多個excel。

二是呼叫writetodownload方法

在同乙個excel中建多個sheet。

若在同一excel中建多個sheet,若記錄數達數十萬,會導致位元組流溢位的問題,解決辦法是先獲取memorystream,然後分塊讀取寫入檔案流。

需要注意的是在讀取記憶體流的時候,一定要將記憶體流的位置設為0,因為在從hssfworkbook中獲取記憶體流時,位置已經置於最後了!若不重position重新置為0則讀取不到任何資料。

**

using

system;

using

system.collections.generic;

using

luceservicewinform.common;

using

npoi.hssf.usermodel;

using

npoi.hpsf;

using

system.web;

using

system.io;

using

system.data;

using

npoi.ss.usermodel;

using

system.reflection;

namespace

luceservicewinform.helper

//////

datatable寫入excel

/// ///

要儲存的檔名稱 eg:test.xls

///工作薄名稱

///要寫入的datatable

//初始化excel資訊

initializeworkbook();

//填充資料

當大於65536條記錄時,**中建多個sheet

/// ///

實體 ///

要儲存的檔名稱 eg:test.xls

///工作薄名稱

///要寫入的list

public

static

void writetodownload(string filename, string sheetname, listlst, listlisttitle)

public

static

void writetodownload(string dir, string filename, string sheetname, listlst, listlisttitle)

//初始化excel資訊

initializeworkbook();

//填充資料

listexcel(sheetname, lst, listtitle);

memorystream memorystream =writetostream();

filestream fstr = new filestream(dir + "

\\" + filename + datetime.now.tostring("

yymmddhhmmss

") + "

.xls

", filemode.openorcreate, fileaccess.write);

writememorystream(memorystream, fstr);

}catch

(exception ex)

}//////

將源記憶體流寫入目標記憶體流

/// ///

源記憶體流

///目標檔案流

private

static

void

writememorystream(memorystream memorystream, filestream filestream)

else}}

}}

catch

(exception ex)

}//////

從hssfworkbook中獲取記憶體流

/// ///

static

memorystream writetostream()

catch

(exception ex)

return

file;

}#region 資料填充部分

//////將datatable資料寫入到excel

/// ///

//////

static

void dtexcel(string sheetname, datatable dt, listlsttitle)

}irow hstitlerow = sheet1.createrow(0

);

//標題賦值

for (int yt = 0; yt < lsttitle.count; yt++)

//填充資料項

for (int xcount = 1; xcount < x; xcount++)}}

private

static

int index = 0

;

static

void fill65536(string sheetname, listlst, listlsttitle)

else

}static

void listexcel(string sheetname, listlst, listlsttitle)

}irow hstitlerow = sheet1.createrow(0

);

//標題賦值

for (int yt = 0; yt < lsttitle.count; yt++)

//填充資料項

for (int xcount = 1; xcount <= lst.count; xcount++)

else}}

}#endregion

//////

當大於65536條記錄時,建多個excel

/// ///

//////

//////

///public

static

void writetodownload65536(string folder, string filename, string sheetname, listlist, listlisttitle)

else}}

}

npoihelper

C 使用NPOI 匯出Excel

npoi可以在沒有安裝office的情況下對word或excel文件進行讀寫操作 下面介紹下npoi操作excel的方法 這裡使用的是net4.0 將下面幾個dll新增到專案中並引用 廢話不多說 上 create 2016 11 30 by sly 要匯入的資料 datatable的列名是否要匯入 ...

C 使用NPOI的Excel匯出

前言 開發環境 vs2017 sqlserver 谷歌瀏覽器 正文1.在專案中引用npoi a.在neget裡搜尋npoi直接安裝 推薦 2.建立個類,內容如下 public byte createexcel datatable datatable,string name 輸入資料行 for int...

C ,使用NPOI,匯出excel檔案

匯出excel檔案 table表資料 存放路徑 string path system.environment.currentdirectory public void exportwps excel datatable dt,string path endregion region 資料填充 int...