| | |
| | | * 标题行号 |
| | | */ |
| | | private int headerNum; |
| | | /** |
| | | * 标题行号 |
| | | */ |
| | | private CellType changeType; |
| | | |
| | | /** |
| | | * 构造函数 |
| | |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | | * 构造函数 () |
| | |
| | | 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."); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 获取行对象 |
| | |
| | | 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){ |