Skip to content

Commit

Permalink
perf($POI): update Apache POI version to 5.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Feb 18, 2021
1 parent abf0543 commit 7b60af5
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 148 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<jjwt.version>0.11.1</jjwt.version>
<java-faker.version>1.0.2</java-faker.version>
<easyexcel.version>2.2.6</easyexcel.version>
<poi.version>5.0.0</poi.version>
</properties>

<!-- The modules (sometimes called subprojects) to build as a part of this project. -->
Expand Down
12 changes: 9 additions & 3 deletions spring-cloud-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,15 @@
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>${easyexcel.version}</version>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,222 +11,169 @@
* <p>
* 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<Row> rowIt = fromSheet.rowIterator(); rowIt.hasNext(); ) {
mergerRegion(sourceSheet, targetSheet);
for (Iterator<Row> 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<Cell> cellIt = fromRow.cellIterator(); cellIt.hasNext(); ) {

public static void copyRow(HSSFWorkbook workbook, HSSFRow sourceRow, HSSFRow targetRow, boolean copyValue) {
for (Iterator<Cell> 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<Cell> cellIt = fromRow.cellIterator(); cellIt.hasNext(); ) {
public static void copyRow(boolean copyValue, Workbook workbook, Row sourceRow, Row targetRow) {
targetRow.setHeight(sourceRow.getHeight());

for (Iterator<Cell> 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:
}
Expand Down

0 comments on commit 7b60af5

Please sign in to comment.