-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf($POI): update Apache POI version to 5.0.0
- Loading branch information
1 parent
abf0543
commit 7b60af5
Showing
4 changed files
with
102 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,222 +11,169 @@ | |
* <p> | ||
* Change description here. | ||
* | ||
* @author 钟俊 (jun.zhong), email: [email protected] | ||
* @date 4 /30/20 6:37 PM | ||
* @author @author Johnny Miller (锺俊), email: [email protected], 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: | ||
} | ||
|