jiangping
2025-02-18 b33f2e78ffd4fd401698b84a33dc3c293793c62f
server/system_service/src/main/java/com/doumee/core/annotation/excel/ExcelImporter.java
@@ -38,6 +38,10 @@
     * 标题行号
     */
    private int headerNum;
    /**
     * 标题行号
     */
    private CellType changeType;
    /**
     * 构造函数
@@ -101,6 +105,18 @@
        throws InvalidFormatException, IOException {
        this(multipartFile.getOriginalFilename(), multipartFile.getInputStream(), headerNum, sheetIndex);
    }
    /**
     * 构造函数 (重要)
     * @param--file 导入文件对象
     * @param headerNum 标题行号,数据行号=标题行号+1
     * @param sheetIndex 工作表编号
     * @throws InvalidFormatException
     * @throws IOException
     */
    public ExcelImporter(MultipartFile multipartFile, int headerNum, int sheetIndex, CellType cellType)
        throws InvalidFormatException, IOException {
        this(multipartFile.getOriginalFilename(), multipartFile.getInputStream(), headerNum, sheetIndex,cellType);
    }
    /**
     * 构造函数 ()
@@ -128,6 +144,27 @@
        this.headerNum = headerNum;
        log.debug("Initialize success.");
    }
    public ExcelImporter(String fileName, InputStream in, int headerNum, int sheetIndex,CellType cellType)
        throws InvalidFormatException, IOException {
        if (StringUtils.isBlank(fileName)){
            throw new RuntimeException("导入文档为空!");
        }else if(fileName.toLowerCase().endsWith("xls")){
            this.wb = new HSSFWorkbook(in);
        }else if(fileName.toLowerCase().endsWith("xlsx")){
            this.wb = new XSSFWorkbook(in);
        }else{
            throw new RuntimeException("文档格式不正确!");
        }
        if (this.wb.getNumberOfSheets()<sheetIndex){
            throw new RuntimeException("文档中没有工作表!");
        }
        this.sheet = this.wb.getSheetAt(sheetIndex);
        this.headerNum = headerNum;
        this.changeType = cellType;
        log.debug("Initialize success.");
    }
    /**
     * 获取行对象
@@ -174,7 +211,8 @@
            Cell cell = row.getCell(column);
            if (cell != null){
                if (cell.getCellType() == CellType.NUMERIC){
                    val = cell.getNumericCellValue();
                    cell.setCellType(CellType.STRING); // 确保单元格类型为字符串
                    val = cell.getStringCellValue();
                }else if (cell.getCellType() == CellType.STRING){
                    val = cell.getStringCellValue();
                }else if (cell.getCellType() == CellType.FORMULA){
@@ -295,7 +333,8 @@
                            }else{
                                val = String.valueOf(val.toString()).trim();
                            }
                        }else if (valType == Integer.class){
                        }
                        else if (valType == Integer.class){
                            val = Double.valueOf(val.toString()).intValue();
                        }else if (valType == Long.class){
                            val = Double.valueOf(val.toString()).longValue();