首先是公共方法
private String getCellValueAsString(Cell cell) {if (cell == null) {return "";}String value = "";switch (cell.getCellType()) {case STRING:value = cell.getStringCellValue();break;case NUMERIC:if (DateUtil.isCellDateFormatted(cell)) {value = cell.getDateCellValue().toString();} else {value = Double.toString(cell.getNumericCellValue());}break;case BOOLEAN:value = Boolean.toString(cell.getBooleanCellValue());break;case FORMULA:value = cell.getCellFormula();break;default:return "";}// 移除所有换行符return value.replaceAll("\\r\\n|\\n|\\r", "");}/*** 解析联锁表表格的公共方法* @param sheetIndex 要解析的第几个表格* @param clazz 解析对象* @param mapper* @param passTitle 需要跳过的表头行数* @param <T>*/public <T> void importJlLsb(Integer sheetIndex, Class<T> clazz, BiFunction<T, Cell, T> mapper, Integer passTitle) throws IOException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {String filePath = "D:/project/联锁表格式.xlsx";List<T> jlLSBList = new ArrayList<>();FileInputStream inputStream = new FileInputStream(new File(filePath));Workbook workbook = new XSSFWorkbook(inputStream);Sheet firstSheet = workbook.getSheetAt(sheetIndex);Iterator<Row> iterator = firstSheet.iterator();// 跳过标题行for (int i = 0; i < passTitle; i++) {iterator.next();}while (iterator.hasNext()) {Row nextRow = iterator.next();T jlLSB = clazz.getDeclaredConstructor().newInstance();// 遍历列for (int i = 0; i < nextRow.getLastCellNum(); i++) {Cell cell = nextRow.getCell(i);if (cell == null || cell.getCellType() == CellType.BLANK) {// 检查是否在合并单元格中for (CellRangeAddress range : firstSheet.getMergedRegions()) {if (range.isInRange(nextRow.getRowNum(), i)) {// 获取合并单元格的第一个单元格的值Row firstRow = firstSheet.getRow(range.getFirstRow());Cell firstCell = firstRow.getCell(range.getFirstColumn());// 设置对象的属性jlLSB = mapper.apply(jlLSB, firstCell);break;}}} else {// 设置对象的属性jlLSB = mapper.apply(jlLSB, cell);}}// 输出对象的属性System.out.println(jlLSB.toString());jlLSBList.add(jlLSB);}workbook.close();inputStream.close();}
然后是是调用方法,这里使用了两个对象,对应两个表格
public void ImportLcJlLsb() throws IOException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {importJlLsb(0, LcJlLSB.class, (jlLSB, cell) -> {LcJlLSB lcJlLSB = (LcJlLSB) jlLSB;setLcJlLSBProperty(lcJlLSB, cell, cell.getColumnIndex());return lcJlLSB;}, 2);}private void setLcJlLSBProperty(LcJlLSB lcJlLSB, Cell cell, int columnIndex) {// 根据列索引设置对象的属性switch (columnIndex) {case 0:lcJlLSB.setDirectionOne(getCellValueAsString(cell));break;case 1:lcJlLSB.setDirectionTow(getCellValueAsString(cell));break;case 3:lcJlLSB.setJlNumber(getCellValueAsString(cell));break;// ... 其他字段}}public void ImportYxJlLsb() throws IOException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {importJlLsb(1, YxJlLSB.class, (jlLSB, cell) -> {YxJlLSB yxJlLSB = (YxJlLSB) jlLSB;setYxJlLSBProperty(yxJlLSB, cell, cell.getColumnIndex());return yxJlLSB;}, 2);}private void setYxJlLSBProperty(YxJlLSB yxJlLSB, Cell cell, int columnIndex) {// 根据列索引设置对象的属性switch (columnIndex) {case 0:yxJlLSB.setDirectionOne(getCellValueAsString(cell));break;case 1:yxJlLSB.setDirectionTow(getCellValueAsString(cell));break;case 3:yxJlLSB.setJlNumber(getCellValueAsString(cell));break;// ... 其他字段}}