jar
public static void readXLSX(String path) throws FileNotFoundException, IOException{ //读取工作表 XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(path)); // 循环工作表Sheet for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) { XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet); if (xssfSheet == null) { continue; } // 循环行Row for (int rowNum = 0; rowNum <= xssfSheet.getLastRowNum(); rowNum++) { // System.out.println("正运行到"+rowNum+"行了"); // 获得行 XSSFRow xssfRow = xssfSheet.getRow(rowNum); if (xssfRow == null) { continue; } int cells = xssfRow.getPhysicalNumberOfCells(); for (int i = 0; i < cells; i++) { //列 XSSFCell oneCell = xssfRow.getCell(i); System.out.print(oneCell + "\t"); if (oneCell == null) { continue; } } System.out.println(); } } }
@SuppressWarnings("static-access") private static String getValue(XSSFCell xssfCell) { if (xssfCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) { return String.valueOf(xssfCell.getBooleanCellValue()); } else if (xssfCell.getCellType() == xssfCell.CELL_TYPE_NUMERIC) { return String.valueOf(xssfCell.getNumericCellValue()); } else { return String.valueOf(xssfCell.getStringCellValue()); } }