NPoI用于Excel表的导出,导入,
使用NPoI需要引入dll文件,Npoi.dll和lonic.zip.dll.对于开发者主要使用Npoi.Hssf.userModel空间下的,HSSfWorkbook,HSSfSheet,HSSfRow,HSSfCell,对应的借口在Npoi.ss.UserModel空间下的, iworkbook,iSheet,IRow,ICell,分别对应Excel文件,工作薄,行,列。
简单演示一下创建一个Workbook对象,添加一个工作表,在工作表中添加一行一列:
1
2
3
4
5
6
7
8
9
10
11
12
13
14 |
using
NPOI.HSSF.UserModel; using
NPOI.SS.UserModel; public
class NPOIWrite { void
CreateSheet() { IWorkbook workbook = new
HSSFWorkbook(); //创建Workbook对象 ISheet sheet = workbook.CreateSheet( "Sheet1" ); //创建工作表 IRow row = sheet.CreateRow(0); //在工作表中添加一行 ICell cell = row.CreateCell(0); //在行中添加一列 cell.SetCellValue( "test" ); //设置列的内容 } } |
相应的读取代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
using
System.IO; using
NPOI.HSSF.UserModel; using
NPOI.SS.UserModel; public
class NPOIRead { void
GetSheet(Stream stream) { IWorkbook workbook = new
HSSFWorkbook(stream); //从流内容创建Workbook对象 ISheet sheet = workbook.GetSheetAt(0); //获取第一个工作表 IRow row = sheet.GetRow(0); //获取工作表第一行 ICell cell = row.GetCell(0); //获取行的第一列 string
value = cell.ToString(); //获取列的值 } } |
从DataTable读取内容来创建Workbook对象:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 |
public
static MemoryStream RenderToExcel(DataTable table) { MemoryStream ms = new
MemoryStream(); using
(table) { using
(IWorkbook workbook = new
HSSFWorkbook()) { using
(ISheet sheet = workbook.CreateSheet()) { IRow headerRow = sheet.CreateRow(0); // handling header. foreach
(DataColumn column in
table.Columns) headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption); //If Caption not set, returns the ColumnName value // handling value. int
rowIndex = 1; foreach
(DataRow row in
table.Rows) { IRow dataRow = sheet.CreateRow(rowIndex); foreach
(DataColumn column in
table.Columns) { dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString()); } rowIndex++; } workbook.Write(ms); ms.Flush(); ms.Position = 0; } } } return
ms; } |
需要注意的是,sheet.LastRowNum = sheet.PhysicalNumberOfRows - 1,这里可能存在BUG:当没有数据或只有一行数据时sheet.LastRowNum为0,PhysicalNumberOfRows 表现正常。
这里读取流中的Excel来创建Workbook对象,并转换成DataTable:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 |
static
DataTable RenderFromExcel(Stream excelFileStream) { using
(excelFileStream) { using
(IWorkbook workbook = new
HSSFWorkbook(excelFileStream)) { using
(ISheet sheet = workbook.GetSheetAt(0)) //取第一个表 { DataTable table = new
DataTable(); IRow headerRow = sheet.GetRow(0); //第一行为标题行 int
cellCount = headerRow.LastCellNum; //LastCellNum = PhysicalNumberOfCells int
rowCount = sheet.LastRowNum; //LastRowNum = PhysicalNumberOfRows - 1 //handling header. for
( int
i = headerRow.FirstCellNum; i < cellCount; i++) { DataColumn column = new
DataColumn(headerRow.GetCell(i).StringCellValue); table.Columns.Add(column); } for
( int
i = (sheet.FirstRowNum + 1); i <= rowCount; i++) { IRow row = sheet.GetRow(i); DataRow dataRow = table.NewRow(); if
(row != null ) { for
( int
j = row.FirstCellNum; j < cellCount; j++) { if
(row.GetCell(j) != null ) dataRow[j] = GetCellValue(row.GetCell(j)); } } table.Rows.Add(dataRow); } return
table; } } } } |
原文:http://www.cnblogs.com/chm74/p/3642646.html