我有一些代码写入Excel文件:
File file = new File("Path here");
File to = new File("Path here");
Workbook workbook = Workbook.getWorkbook(file);
WritableWorkbook target = Workbook.createWorkbook(to);
target.createSheet("s", 0);
Sheet source = workbook.getSheet(1);
WritableSheet targetSheet = target.getSheet(0);
copyRow(source, 1, targetSheet, 1);
workbook.close();
target.close();
请注意,Path here
将替换为实际代码中的实际路径。
copyRow()
方法如下所示:
private void copyRow(Sheet sourceSheet, int sourceRow, WritableSheet targetSheet, int targetRow) throws WriteException {
for(int i = 0, numOfColumn = sourceSheet.getColumns(); i < numOfColumn; i++) {
Cell source = sourceSheet.getCell(i, sourceRow);
WritableCell cell = new Label(i, targetRow, source.getContents());
targetSheet.addCell(cell);
}
}
它运行,但是当我尝试打开to
文件时:
当我使用Windows计算机打开同一个文件时,会显示一条错误消息,指出无法打开该文件。
为什么会这样?我该如何解决?