/** * 读取Excel数据 * @param filePath 文件路径 * @param sheetName 工作表名称 * @return 以List<List<String>>形式返回Excel中的数据 * @throws IOException * @throws InvalidFormatException */ public static List<List<String>> readExcelData(String filePath, String sheetName) throws IOException, InvalidFormatException { List<List<String>> data = new ArrayList<>(); // 创建工作簿 Workbook workbook = WorkbookFactory.create(new File(filePath)); // 获取工作表 Sheet sheet = workbook.getSheet(sheetName); // 获取行数 int rowCount = sheet.getPhysicalNumberOfRows(); for (int i = 0; i < rowCount; i++) { Row row = sheet.getRow(i); if (row != null) { int columnCount = row.getLastCellNum(); List<String> rowData = new ArrayList<>(); for (int j = 0; j < columnCount; j++) { Cell cell = row.getCell(j); if (cell != null) { // 根据单元格数据类型获取值 switch (cell.getCellType()) { case STRING: rowData.add(cell.getStringCellValue()); break; case NUMERIC: rowData.add(Double.toString(cell.getNumericCellValue())); break; case BOOLEAN: rowData.add(Boolean.toString(cell.getBooleanCellValue())); break; case FORMULA: rowData.add(cell.getCellFormula()); break; default: rowData.add(""); } } else { rowData.add(""); } } data.add(rowData); } } return data; }
标签:java,读取,int,Excel,cell,add,rowData,row From: https://www.cnblogs.com/JErryID/p/17220444.html