Apache-POI 简单应用
测试的Excel文件为四列的普通表格
jar包:poi-3.15-beta2.jar(Office2003xls文件)、poi-ooxml-3.15-beta2.jar(Office2007xlsx文件)
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 | File file= new File( "D:\\testEXC.xlsx" ); if (!file.exists()) { throw new FileNotFoundException(); } FileInputStream fis = new FileInputStream(file); @SuppressWarnings ( "resource" ) XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fis); int sheetNum = xssfWorkbook.getNumberOfSheets(); for ( int i = 0 ;i<sheetNum;i++) { Sheet sheet = xssfWorkbook.getSheetAt(i); int lastRow = sheet.getLastRowNum(); if (lastRow <= 0 ) { continue ; } Row row = null ; String name = "" ; String title = "" ; String content = "" ; String email = "" ; for ( int rowNum = 1 ;rowNum <= lastRow;rowNum++) { row = sheet.getRow(rowNum); if (row == null ) { continue ; } name = getCellValue(row.getCell( 0 )); title = getCellValue(row.getCell( 1 )); content = getCellValue(row.getCell( 2 )); email = getCellValue(row.getCell( 3 )); System.out.println(name+ " / " +title+ " / " +content+ " / " +email); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步