Excel2003-97
1 static void NPOI_ImportExcel2003(Args _args) 2 { 3 System.IO.FileStream file=new System.IO.FileStream("D:\test.xls", System.IO.FileMode::Open,System.IO.FileAccess::Read); 4 NPOI.HSSF.UserModel.HSSFWorkbook workBook= new NPOI.HSSF.UserModel.HSSFWorkbook(file); 5 NPOI.SS.UserModel.ISheet sheet=workBook.GetSheetAt(0); 6 NPOI.SS.UserModel.IRow row; 7 NPOI.SS.UserModel.ICell cell; 8 int i,j,t,rowCount,cellCount; 9 ; 10 t=timenow(); 11 12 rowCount=sheet.get_LastRowNum(); 13 for(i=0;i<rowCount;i++) 14 { 15 row=sheet.GetRow(i); 16 cellCount=row.get_LastCellNum(); 17 for(j=0;j<cellCount;j++) 18 { 19 cell=row.GetCell(j); 20 print cell.get_StringCellValue(); 21 } 22 } 23 file.Close(); 24 25 info(int2str(timenow()-t)); 26 }
Excel2007
跟2003-97版本差不多,只是workBook引用的class不一样(红色部分)
1 static void NPOI_ImportExcel2007(Args _args) 2 { 3 System.IO.FileStream file=new System.IO.FileStream("D:\test.xlsx", System.IO.FileMode::Open,System.IO.FileAccess::Read); 4 NPOI.XSSF.UserModel.XSSFWorkbook workBook= new NPOI.XSSF.UserModel.XSSFWorkbook(file); 5 NPOI.SS.UserModel.ISheet sheet=workBook.GetSheetAt(0); 6 NPOI.SS.UserModel.IRow row; 7 NPOI.SS.UserModel.ICell cell; 8 int i,j,t,rowCount,cellCount; 9 ; 10 t=timenow(); 11 12 rowCount=sheet.get_LastRowNum(); 13 for(i=0;i<rowCount;i++) 14 { 15 row=sheet.GetRow(i); 16 cellCount=row.get_LastCellNum(); 17 for(j=0;j<cellCount;j++) 18 { 19 cell=row.GetCell(j); 20 print cell.get_StringCellValue(); 21 } 22 } 23 file.Close(); 24 25 info(int2str(timenow()-t)); 26 }