要使用Apache POI和Java将一个工作簿的工作表复制到另一个工作簿表,您可以按照以下步骤操作:
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.1</version>
</dependency><dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.1</version>
</dependency>
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public void copyWorksheet(String sourceWorkbookPath, String targetWorkbookPath) throws IOException {
// 打开源工作簿
try (FileInputStream inputStream = new FileInputStream(new File(sourceWorkbookPath))) {
Workbook sourceWorkbook = new XSSFWorkbook(inputStream);
// 打开目标工作簿
try (FileInputStream targetInputStream = new FileInputStream(new File(targetWorkbookPath))) {
Workbook targetWorkbook = new XSSFWorkbook(targetInputStream);
// 复制工作表
Sheet sourceSheet = sourceWorkbook.getSheetAt(0);
Sheet targetSheet = targetWorkbook.createSheet(sourceSheet.getSheetName());
copySheet(sourceSheet, targetSheet, sourceWorkbook, targetWorkbook);
// 保存目标工作簿
try (FileOutputStream outputStream = new FileOutputStream(new File(targetWorkbookPath))) {
targetWorkbook.write(outputStream);
}
}
}
}
copySheet
方法,该方法将接受源工作表、目标工作表、源工作簿和目标工作簿作为参数。在此方法中,您将复制源工作表的内容到目标工作表。private void copySheet(Sheet sourceSheet, Sheet targetSheet, Workbook sourceWorkbook, Workbook targetWorkbook) {
// 复制行
for (Row sourceRow : sourceSheet) {
Row targetRow = targetSheet.createRow(sourceRow.getRowNum());
copyRow(sourceRow, targetRow, sourceWorkbook, targetWorkbook);
}
// 复制列样式
for (int i = 0; i< sourceSheet.getNumMergedRegions(); i++) {
CellRangeAddress sourceRegion = sourceSheet.getMergedRegion(i);
CellRangeAddress targetRegion = new CellRangeAddress(sourceRegion.getFirstRow(), sourceRegion.getLastRow(),
sourceRegion.getFirstColumn(), sourceRegion.getLastColumn());
targetSheet.addMergedRegion(targetRegion);
}
}
copyRow
方法,该方法将接受源行、目标行、源工作簿和目标工作簿作为参数。在此方法中,您将复制源行的内容到目标行。private void copyRow(Row sourceRow, Row targetRow, Workbook sourceWorkbook, Workbook targetWorkbook) {
// 复制单元格
for (Cell sourceCell : sourceRow) {
Cell targetCell = targetRow.createCell(sourceCell.getColumnIndex());
copyCell(sourceCell, targetCell, sourceWorkbook, targetWorkbook);
}
// 复制行样式
targetRow.setHeight(sourceRow.getHeight());
}
copyCell
方法,该方法将接受源单元格、目标单元格、源工作簿和目标工作簿作为参数。在此方法中,您将复制源单元格的内容到目标单元格。private void copyCell(Cell sourceCell, Cell targetCell, Workbook sourceWorkbook, Workbook targetWorkbook) {
// 复制单元格样式
targetCell.setCellStyle(sourceWorkbook.getCellStyleAt(sourceCell.getCellStyle().getIndex()));
// 复制单元格值
switch (sourceCell.getCellType()) {
case STRING:
targetCell.setCellValue(sourceCell.getStringCellValue());
break;
case NUMERIC:
targetCell.setCellValue(sourceCell.getNumericCellValue());
break;
case BOOLEAN:
targetCell.setCellValue(sourceCell.getBooleanCellValue());
break;
case FORMULA:
targetCell.setCellFormula(sourceCell.getCellFormula());
break;
case BLANK:
targetCell.setCellValue("");
break;
default:
break;
}
}
现在,您可以使用copyWorksheet
方法将源工作簿的工作表复制到目标工作簿的工作表。例如:
public static void main(String[] args) {
String sourceWorkbookPath = "source.xlsx";
String targetWorkbookPath = "target.xlsx";
new ApachePOIExample().copyWorksheet(sourceWorkbookPath, targetWorkbookPath);
}
这将复制源工作簿的第一个工作表到目标工作簿的第一个工作表。如果您需要复制其他工作表,请相应地更改getSheetAt
方法的参数。
领取专属 10元无门槛券
手把手带您无忧上云