java poi 怎么复制sheet
时间: 2023-05-11 16:06:08 浏览: 119
你可以使用以下代码复制一个sheet:
```java
// 获取要复制的sheet
Sheet sheetToCopy = workbook.getSheet("Sheet1");
// 创建一个新的sheet
Sheet newSheet = workbook.createSheet("New Sheet");
// 复制sheet
int maxColumnNum = 0;
for (int i = sheetToCopy.getFirstRowNum(); i <= sheetToCopy.getLastRowNum(); i++) {
Row srcRow = sheetToCopy.getRow(i);
Row destRow = newSheet.createRow(i);
if (srcRow != null) {
for (int j = srcRow.getFirstCellNum(); j < srcRow.getLastCellNum(); j++) {
Cell oldCell = srcRow.getCell(j);
Cell newCell = destRow.createCell(j);
if (oldCell != null) {
newCell.setCellStyle(oldCell.getCellStyle());
newCell.setCellType(oldCell.getCellType());
switch (oldCell.getCellType()) {
case BOOLEAN:
newCell.setCellValue(oldCell.getBooleanCellValue());
break;
case ERROR:
newCell.setCellErrorValue(oldCell.getErrorCellValue());
break;
case FORMULA:
newCell.setCellFormula(oldCell.getCellFormula());
break;
case NUMERIC:
newCell.setCellValue(oldCell.getNumericCellValue());
break;
case STRING:
newCell.setCellValue(oldCell.getRichStringCellValue());
break;
}
}
}
if (srcRow.getLastCellNum() > maxColumnNum) {
maxColumnNum = srcRow.getLastCellNum();
}
}
}
for (int i = 0; i <= maxColumnNum; i++) {
newSheet.setColumnWidth(i, sheetToCopy.getColumnWidth(i));
}
```
希望这能帮到你!
阅读全文