using (FileStream fs = File.OpenRead("1.xls")) { IWorkbook wk = new XSSFWorkbook(fs); //读取03的xls //IWorkbook wk = new HSSFWorkbook(); //读取07之xlsx for (int i = 0; i < wk.NumberOfSheets; i++) { ISheet sheet = wk.GetSheetAt(i); Console.WriteLine("============{0}==Start============", sheet.SheetName); for (int j = 0; j <= sheet.LastRowNum; j++) { IRow row = sheet.GetRow(j); for (int k = 0; k < row.LastCellNum; k++) { ICell cell = row.GetCell(k); Console.WriteLine(cell.ToString() + "\t"); } } Console.WriteLine("============{0}==End==============", sheet.SheetName); } }
一楼那里有下载地址,npoi使用很简单的,你先自己试试吧,不行的话,在发消息
IWorkbook没有啊
@淘@淘: IWorkbook接口是在using NPOI.SS.UserModel;命名空间下,实现不好意思,注释写反了,对不住哈!我刚才又重新试了一下,没问题
using (FileStream fs = File.OpenRead("1.xls")) { //IWorkbook wk = new XSSFWorkbook(fs); //读取07的xls IWorkbook wk = new HSSFWorkbook(fs); //读取03之xlsx for (int i = 0; i < wk.NumberOfSheets; i++) { ISheet sheet = wk.GetSheetAt(i); Console.WriteLine("============{0}==Start============", sheet.SheetName); for (int j = 0; j <= sheet.LastRowNum; j++) { IRow row = sheet.GetRow(j); for (int k = 0; k < row.LastCellNum; k++) { ICell cell = row.GetCell(k); Console.WriteLine(cell.ToString() + "\t"); } } Console.WriteLine("============{0}==End==============", sheet.SheetName); } }
上面是读取的,下面这个是写入的(比较简单)
//IWorkbook wk = new XSSFWorkbook(); //写07之后xlsx IWorkbook wk = new HSSFWorkbook(); ISheet sheet = wk.CreateSheet("hhh"); IRow row1 = sheet.CreateRow(0); for (int i = 0; i < 10; i++) { row1.CreateCell(i).SetCellValue("create" + i); } IRow row2 = sheet.CreateRow(1); for (int i = 0; i < 8; i++) { row2.CreateCell(i).SetCellValue("delete" + i); } using (FileStream fs = File.OpenWrite("1213.xls")) { wk.Write(fs); Console.WriteLine("ok"); }
@秋壶冰月: 谢谢 很好用
@淘@淘: 恩 使用比较简单/方便
Npoi 读取和生成 非常快,微软组件太慢了,又不好操作。
支持NPOI