NPOI操作Excel時使用列頭來讀取資料的方法

2022-03-31 17:18:09 字數 867 閱讀 5020

首先定義擴充套件方法:

1

public

static icell getcell(this irow row, string

clounmname)210

}11throw

new exception(string.format("

未找到名為\"\"的列頭

", clounmname));

12 }

呼叫:

filestream fs = new

filestream(filename, filemode.open)

iworkbook workbook = new

npoi.xssf.usermodel.xssfworkbook(fs);

string value = string

.empty;

isheet sheet = workbook.getsheetat(0

);for (int i = 1; i < sheet.physicalnumberofrows; i++)

附:擴充套件方法的使用:

擴充套件方法使你能夠向現有型別「新增」方法,而無需建立新的派生型別、重新編譯或以其他方式修改原始型別。  擴充套件方法是一種特殊的靜態方法,但可以像擴充套件型別上的例項方法一樣進行呼叫。

1

//建立自己的靜態類

2public

static

class

myextension

39 }

1

class

program

29 }

使用NPOI進行Excel操作

一 npoi元件匯入 右鍵專案選單,管理nuget程式包 安裝完成後專案引用會出現以下幾項 二 基礎使用 新增引用 using npoi.hpsf using npoi.hssf.usermodel using npoi.ss.usermodel excel操作開始 hssfworkbook hss...

NPOI操作Excel 一 NPOI基礎

用c 讀取excel的方法有很多中,由於近期工作需要,需要解析的excel含有合併單元格以及背景色等特殊要求,故在網上查了一些關於讀excel的方法的優缺點,覺得npoi能滿足我的需要,所以搜尋了一些資料做了一些測試,下面有些內容有些是 於別人的部落格,都備有出處,只是想把一些覺得對自己有用的記錄一...

使用NPOI操作Excel 03 07

1 using system 2using system.collections.generic 3using system.linq 4using system.text 5using npoi.ss.usermodel 6using npoi.xssf.usermodel 7using npoi...