From 7b60af5ba65113c32c48520e94ac2758326032b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20Miller=20=28=E9=94=BA=E4=BF=8A=29?= Date: Thu, 18 Feb 2021 17:15:29 +0800 Subject: [PATCH] perf($POI): update Apache POI version to 5.0.0 --- pom.xml | 1 + spring-cloud-starter/pom.xml | 12 +- .../AbstractExcelImportController.java | 4 +- .../maf/springcloudstarter/util/PoiUtil.java | 233 +++++++----------- 4 files changed, 102 insertions(+), 148 deletions(-) diff --git a/pom.xml b/pom.xml index a8611d12..6702fbdf 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,7 @@ 0.11.1 1.0.2 2.2.6 + 5.0.0 diff --git a/spring-cloud-starter/pom.xml b/spring-cloud-starter/pom.xml index 5b5ff67b..ff813f85 100644 --- a/spring-cloud-starter/pom.xml +++ b/spring-cloud-starter/pom.xml @@ -125,9 +125,15 @@ - com.alibaba - easyexcel - ${easyexcel.version} + org.apache.poi + poi + ${poi.version} + + + + org.apache.poi + poi-ooxml + ${poi.version} diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/controller/AbstractExcelImportController.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/controller/AbstractExcelImportController.java index e7643620..ce84bb34 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/controller/AbstractExcelImportController.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/controller/AbstractExcelImportController.java @@ -675,7 +675,7 @@ private boolean isBlankRow(Row row) { Cell cell = row.getCell(i, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL); String value = ""; if (cell != null) { - switch (cell.getCellTypeEnum()) { + switch (cell.getCellType()) { case STRING: value = cell.getStringCellValue(); break; @@ -793,7 +793,7 @@ private void bindRowToBean(Row row, int startIndex, int endIndex) { private String getCellValue2String(Cell cell) { String returnString = ""; if (cell != null) { - switch (cell.getCellTypeEnum()) { + switch (cell.getCellType()) { case BLANK: return ""; case NUMERIC: diff --git a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/util/PoiUtil.java b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/util/PoiUtil.java index f572ac26..7f730f23 100644 --- a/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/util/PoiUtil.java +++ b/spring-cloud-starter/src/main/java/com/jmsoftware/maf/springcloudstarter/util/PoiUtil.java @@ -11,222 +11,169 @@ *

* Change description here. * - * @author 钟俊 (jun.zhong), email: jun.zhong@ucarinc.com - * @date 4 /30/20 6:37 PM + * @author @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 2/18/2021 5:37 PM */ +@SuppressWarnings({"AlibabaRemoveCommentedCode", "unused"}) public class PoiUtil { - /** - * Copy cell style. - * - * @param fromStyle the from style - * @param toStyle the to style - */ - public static void copyCellStyle(HSSFCellStyle fromStyle, - HSSFCellStyle toStyle) { - toStyle.setAlignment(fromStyle.getAlignmentEnum()); - //边框和边框颜色 - toStyle.setBorderBottom(fromStyle.getBorderBottomEnum()); - toStyle.setBorderLeft(fromStyle.getBorderLeftEnum()); - toStyle.setBorderRight(fromStyle.getBorderRightEnum()); - toStyle.setBorderTop(fromStyle.getBorderTopEnum()); - toStyle.setTopBorderColor(fromStyle.getTopBorderColor()); - toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor()); - toStyle.setRightBorderColor(fromStyle.getRightBorderColor()); - toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor()); - - //背景和前景 - toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor()); - toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor()); - - toStyle.setDataFormat(fromStyle.getDataFormat()); - toStyle.setFillPattern(fromStyle.getFillPatternEnum()); -// toStyle.setFont(fromStyle.getFont(null)); - toStyle.setHidden(fromStyle.getHidden()); - toStyle.setIndention(fromStyle.getIndention());//首行缩进 - toStyle.setLocked(fromStyle.getLocked()); - toStyle.setRotation(fromStyle.getRotation());//旋转 - toStyle.setVerticalAlignment(fromStyle.getVerticalAlignmentEnum()); - toStyle.setWrapText(fromStyle.getWrapText()); + public static void copyCellStyle(HSSFCellStyle sourceCellStyle, HSSFCellStyle targetCellStyle) { + targetCellStyle.setAlignment(sourceCellStyle.getAlignment()); + // Boarder style + targetCellStyle.setBorderBottom(sourceCellStyle.getBorderBottom()); + targetCellStyle.setBorderLeft(sourceCellStyle.getBorderLeft()); + targetCellStyle.setBorderRight(sourceCellStyle.getBorderRight()); + targetCellStyle.setBorderTop(sourceCellStyle.getBorderTop()); + targetCellStyle.setTopBorderColor(sourceCellStyle.getTopBorderColor()); + targetCellStyle.setBottomBorderColor(sourceCellStyle.getBottomBorderColor()); + targetCellStyle.setRightBorderColor(sourceCellStyle.getRightBorderColor()); + targetCellStyle.setLeftBorderColor(sourceCellStyle.getLeftBorderColor()); + // Background and foreground + targetCellStyle.setFillBackgroundColor(sourceCellStyle.getFillBackgroundColor()); + targetCellStyle.setFillForegroundColor(sourceCellStyle.getFillForegroundColor()); + // Data format + targetCellStyle.setDataFormat(sourceCellStyle.getDataFormat()); + targetCellStyle.setFillPattern(sourceCellStyle.getFillPattern()); + // toStyle.setFont(fromStyle.getFont(null)); + targetCellStyle.setHidden(sourceCellStyle.getHidden()); + targetCellStyle.setIndention(sourceCellStyle.getIndention()); + targetCellStyle.setLocked(sourceCellStyle.getLocked()); + targetCellStyle.setRotation(sourceCellStyle.getRotation()); + targetCellStyle.setVerticalAlignment(sourceCellStyle.getVerticalAlignment()); + targetCellStyle.setWrapText(sourceCellStyle.getWrapText()); } - /** - * Sheet复制 - * - * @param wb the wb - * @param fromSheet the from sheet - * @param toSheet the to sheet - * @param copyValue the copy value flag - */ - public static void copySheet(HSSFWorkbook wb, HSSFSheet fromSheet, HSSFSheet toSheet, + + public static void copySheet(HSSFWorkbook workbook, HSSFSheet sourceSheet, HSSFSheet targetSheet, boolean copyValue) { - //合并区域处理 - mergerRegion(fromSheet, toSheet); - for (Iterator rowIt = fromSheet.rowIterator(); rowIt.hasNext(); ) { + mergerRegion(sourceSheet, targetSheet); + for (Iterator rowIt = sourceSheet.rowIterator(); rowIt.hasNext(); ) { HSSFRow tmpRow = (HSSFRow) rowIt.next(); - HSSFRow newRow = toSheet.createRow(tmpRow.getRowNum()); - //行复制 - copyRow(wb, tmpRow, newRow, copyValue); + HSSFRow newRow = targetSheet.createRow(tmpRow.getRowNum()); + copyRow(workbook, tmpRow, newRow, copyValue); } } - /** - * 行复制功能 - * - * @param wb the wb - * @param fromRow the from row - * @param toRow the to row - * @param copyValue the copy value flag - */ - public static void copyRow(HSSFWorkbook wb, HSSFRow fromRow, HSSFRow toRow, boolean copyValue) { - for (Iterator cellIt = fromRow.cellIterator(); cellIt.hasNext(); ) { + + public static void copyRow(HSSFWorkbook workbook, HSSFRow sourceRow, HSSFRow targetRow, boolean copyValue) { + for (Iterator cellIt = sourceRow.cellIterator(); cellIt.hasNext(); ) { HSSFCell tmpCell = (HSSFCell) cellIt.next(); - HSSFCell newCell = toRow.createCell(tmpCell.getColumnIndex()); - copyCell(wb, tmpCell, newCell, copyValue); + HSSFCell newCell = targetRow.createCell(tmpCell.getColumnIndex()); + copyCell(workbook, tmpCell, newCell, copyValue); } } + /** - * 复制原有sheet的合并单元格到新创建的sheet + * Merger region. * - * @param fromSheet 原有的sheet - * @param toSheet 新创建sheet + * @param sourceSheet the source sheet + * @param targetSheet the target sheet */ - public static void mergerRegion(HSSFSheet fromSheet, HSSFSheet toSheet) { - int sheetMergerCount = fromSheet.getNumMergedRegions(); + public static void mergerRegion(HSSFSheet sourceSheet, HSSFSheet targetSheet) { + int sheetMergerCount = sourceSheet.getNumMergedRegions(); for (int i = 0; i < sheetMergerCount; i++) { - CellRangeAddress mergedRegionAt = fromSheet.getMergedRegion(i); - toSheet.addMergedRegion(mergedRegionAt); + CellRangeAddress mergedRegionAt = sourceSheet.getMergedRegion(i); + targetSheet.addMergedRegion(mergedRegionAt); } } + /** - * 复制单元格 + * Copy cell. * - * @param wb the wb - * @param srcCell the src cell - * @param distCell the dist cell - * @param copyValue true则连同cell的内容一起复制 + * @param workbook the workbook + * @param sourceCell the source cell + * @param targetCell the target cell + * @param copyValue the copy value */ - public static void copyCell(HSSFWorkbook wb, HSSFCell srcCell, HSSFCell distCell, - boolean copyValue) { - HSSFCellStyle newStyle = wb.createCellStyle(); - copyCellStyle(srcCell.getCellStyle(), newStyle); - //样式 - distCell.setCellStyle(newStyle); - //评论 - if (srcCell.getCellComment() != null) { - distCell.setCellComment(srcCell.getCellComment()); + public static void copyCell(HSSFWorkbook workbook, HSSFCell sourceCell, HSSFCell targetCell, boolean copyValue) { + HSSFCellStyle newStyle = workbook.createCellStyle(); + copyCellStyle(sourceCell.getCellStyle(), newStyle); + targetCell.setCellStyle(newStyle); + if (sourceCell.getCellComment() != null) { + targetCell.setCellComment(sourceCell.getCellComment()); } - // 不同数据类型处理 - CellType srcCellType = srcCell.getCellTypeEnum(); - distCell.setCellType(srcCellType); + CellType srcCellType = sourceCell.getCellType(); + targetCell.setCellType(srcCellType); if (copyValue) { switch (srcCellType) { case NUMERIC: - if (HSSFDateUtil.isCellDateFormatted(srcCell)) { - distCell.setCellValue(srcCell.getDateCellValue()); + if (DateUtil.isCellDateFormatted(sourceCell)) { + targetCell.setCellValue(sourceCell.getDateCellValue()); } else { - distCell.setCellValue(srcCell.getNumericCellValue()); + targetCell.setCellValue(sourceCell.getNumericCellValue()); } break; case STRING: - distCell.setCellValue(srcCell.getRichStringCellValue()); + targetCell.setCellValue(sourceCell.getRichStringCellValue()); break; case BLANK: break; case BOOLEAN: - distCell.setCellValue(srcCell.getBooleanCellValue()); + targetCell.setCellValue(sourceCell.getBooleanCellValue()); break; case ERROR: - distCell.setCellErrorValue(FormulaError.forInt(srcCell.getErrorCellValue())); + targetCell.setCellErrorValue(FormulaError.forInt(sourceCell.getErrorCellValue())); break; case FORMULA: - distCell.setCellFormula(srcCell.getCellFormula()); + targetCell.setCellFormula(sourceCell.getCellFormula()); break; default: } } } - /** - * 行复制功能 - * - * @param copyValue true则连同cell的内容一起复制 - * @param wb 工作簿 - * @param fromRow 从哪行开始 - * @param toRow 目标行 - */ - public static void copyRow(boolean copyValue, Workbook wb, Row fromRow, Row toRow) { - toRow.setHeight(fromRow.getHeight()); - for (Iterator cellIt = fromRow.cellIterator(); cellIt.hasNext(); ) { + public static void copyRow(boolean copyValue, Workbook workbook, Row sourceRow, Row targetRow) { + targetRow.setHeight(sourceRow.getHeight()); + + for (Iterator cellIt = sourceRow.cellIterator(); cellIt.hasNext(); ) { Cell tmpCell = cellIt.next(); - Cell newCell = toRow.createCell(tmpCell.getColumnIndex()); - copyCell(wb, tmpCell, newCell, copyValue); + Cell newCell = targetRow.createCell(tmpCell.getColumnIndex()); + copyCell(workbook, tmpCell, newCell, copyValue); } - Sheet worksheet = fromRow.getSheet(); + Sheet worksheet = sourceRow.getSheet(); for (int i = 0; i < worksheet.getNumMergedRegions(); i++) { CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i); - if (cellRangeAddress.getFirstRow() == fromRow.getRowNum()) { - CellRangeAddress newCellRangeAddress = new CellRangeAddress(toRow.getRowNum(), - (toRow.getRowNum() + (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow())), - cellRangeAddress.getFirstColumn(), - cellRangeAddress.getLastColumn()); + if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) { + CellRangeAddress newCellRangeAddress = + new CellRangeAddress(targetRow.getRowNum(), + (targetRow.getRowNum() + (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow())), + cellRangeAddress.getFirstColumn(), + cellRangeAddress.getLastColumn()); worksheet.addMergedRegion(newCellRangeAddress); } } } - /** - * 复制单元格 - * - * @param wb the wb - * @param srcCell the src cell - * @param distCell the dist cell - * @param copyValue true则连同cell的内容一起复制 - */ - public static void copyCell(Workbook wb, Cell srcCell, Cell distCell, boolean copyValue) { -// CellStyle newStyle = wb.createCellStyle(); -// CellStyle srcStyle = srcCell.getCellStyle(); -// -// newStyle.cloneStyleFrom(srcStyle); -// newStyle.setFont(wb.getFontAt(srcStyle.getFontIndex())); - - // 样式 -// distCell.setCellStyle(srcCell.getCellStyle()); - - // 内容 - if (srcCell.getCellComment() != null) { - distCell.setCellComment(srcCell.getCellComment()); + public static void copyCell(Workbook workbook, Cell sourceCell, Cell targetCell, boolean copyValue) { + if (sourceCell.getCellComment() != null) { + targetCell.setCellComment(sourceCell.getCellComment()); } - - // 不同数据类型处理 - CellType srcCellType = srcCell.getCellTypeEnum(); - distCell.setCellType(srcCellType); + CellType srcCellType = sourceCell.getCellType(); if (copyValue) { switch (srcCellType) { case NUMERIC: - if (HSSFDateUtil.isCellDateFormatted(srcCell)) { - distCell.setCellValue(srcCell.getDateCellValue()); + if (DateUtil.isCellDateFormatted(sourceCell)) { + targetCell.setCellValue(sourceCell.getDateCellValue()); } else { - distCell.setCellValue(srcCell.getNumericCellValue()); + targetCell.setCellValue(sourceCell.getNumericCellValue()); } break; case STRING: - distCell.setCellValue(srcCell.getRichStringCellValue()); + targetCell.setCellValue(sourceCell.getRichStringCellValue()); break; case BLANK: break; case BOOLEAN: - distCell.setCellValue(srcCell.getBooleanCellValue()); + targetCell.setCellValue(sourceCell.getBooleanCellValue()); break; case ERROR: - distCell.setCellErrorValue(srcCell.getErrorCellValue()); + targetCell.setCellErrorValue(sourceCell.getErrorCellValue()); break; case FORMULA: - distCell.setCellFormula(srcCell.getCellFormula()); + targetCell.setCellFormula(sourceCell.getCellFormula()); break; default: }