Java实现一个公共方法解析不同类型的表格

首先是公共方法

    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 firstSheet    表格
     * @param clazz         解析对象
     * @param mapper
     * @param passTitle     需要跳过的表头行数
     * @param <T>
     */
    public <T> void importJlLsb(Sheet firstSheet, Class<T> clazz, BiFunction<T, Cell, T> mapper, Integer passTitle) throws IOException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//        String filePath = "D:/project/联锁表格式.xlsx";   //这三行是解析本地文件的
//        FileInputStream inputStream = new FileInputStream(new File(filePath));
//        Workbook workbook = new XSSFWorkbook(inputStream);
        Iterator<Row> iterator = firstSheet.iterator();
        List<T> jlLSBList = new ArrayList<>();
        // 跳过标题行
        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(MultipartFile file) throws IOException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Workbook workbook = new XSSFWorkbook(file.getInputStream());
        Sheet sheet = workbook.getSheet("列车进路");
        importJlLsb(sheet, LcJlLSB.class, (jlLSB, cell) -> {
            LcJlLSB lcJlLSB = (LcJlLSB) jlLSB;
            setLcJlLSBProperty(lcJlLSB, cell, cell.getColumnIndex());
            return lcJlLSB;
        }, 2);
        workbook.close();
    }

    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(MultipartFile file) throws IOException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Workbook workbook = new XSSFWorkbook(file.getInputStream());
        Sheet sheet = workbook.getSheet("延续进路");
        importJlLsb(sheet, YxJlLSB.class, (jlLSB, cell) -> {
            YxJlLSB yxJlLSB = (YxJlLSB) jlLSB;
            setYxJlLSBProperty(yxJlLSB, cell, cell.getColumnIndex());
            return yxJlLSB;
        }, 2);
        workbook.close();
    }

    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;
            // ... 其他字段
        }
    }

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值