NPOI 是指构建在 POI 3.x 版本之上的一个程序, NPOI 可以在没有安装 Office 的情况下对 Word 或 Excel 文档进行读写操作.
NPOI 是一个开源的 C# 读写 Excel,Word 等微软 OLE2 组件文档的项目.
----------- 摘自百度百科
NPOI 确实是一款在 C# 中很好用的 API, 优点正如上面所说: 可以在没有安装 Office 或者相应环境的机器上对 Word/Excel 文档进行读写
使用代码如下:
- public void test()
- {
- // 获取文件路径
- string rootFromPath = ConfigurationManager.AppSettings["NPOI"];
- // 获取文件名
- string FileName ="123.xlsx";
- // 创建文件对象
- FileStream fileStream = new FileStream(rootFromPath+ @"\" + FileName, FileMode.Open, FileAccess.Read);
- // 创建工作簿对象
- HSSFWorkbook workbook = new HSSFWorkbook(fileStream);
- // 读取工作簿第一张表 (此处参数可为下标, 也可为表名)
- ISheet sheet = workbook.GetSheetAt(0);
- // 新建当前工作表行数据
- IRow row;
- for (int i = 0; i <= sheet.LastRowNum; i++)
- {
- //row 读入第 i 行数据
- row = sheet.GetRow(i);
- // 获取每一列的数据, 并转换为对应的数据类型.
- string c1 = row.GetCell(1).ToString();
- double c2 =System.Convert.ToDouble(row.GetCell(2).ToString()): 0;
- }
- }
来源: http://www.bubuko.com/infodetail-3189412.html