Excel复制sheet页
private Sheet copySheet(Sheet newSheet, Sheet oldSheet, Workbook workbook) {
int numMergedRegions = oldSheet.getNumMergedRegions();
for (int i = 0; i < numMergedRegions; i++) {
CellRangeAddress mergedRegion = oldSheet.getMergedRegion(i);
newSheet.addMergedRegion(mergedRegion);
}
int physicalNumberOfCells = oldSheet.getRow(0).getPhysicalNumberOfCells();
for (int i = 0; i < physicalNumberOfCells; i++) {
newSheet.setColumnWidth(i, 256 * 15);
}
int maxRowSize = oldSheet.getPhysicalNumberOfRows();
for (int i = 0; i <= maxRowSize; i++) {
Row newrow = newSheet.createRow(i);
Row oldRow = oldSheet.getRow(i);
if (oldRow == null) {
continue;
}
int maxColSize = oldRow.getPhysicalNumberOfCells();
for (int j = 0; j < maxColSize; j++) {
Cell newCell = newrow.createCell(j);
Cell oldCell = oldRow.getCell(j);
if (oldCell == null) {
continue;
}
CellType cellType = oldCell.getCellType();
if ("NUMERIC".equals(cellType.name())) {
newCell.setCellValue(oldCell.getNumericCellValue());
} else {
newCell.setCellValue(oldCell.getStringCellValue());
}
CellStyle oldCellStyle = oldCell.getCellStyle();
newCell.setCellStyle(oldCellStyle);
}
}
return newSheet;
}