I would like to copy a sheet from an existing XLS document to a new one in a new location.
How can I do this using JXL?
Workbook w1 = Workbook.getWorkbook(new File("ExistingDocument.xls"), settings);
WritableWorkbook w2 = Workbook.createWorkbook(new File("NewDocument.xls"));
w2.write();
w2.close();
w1.close();
change is
w1.getSheet(0).getCell(0, 0) not WritableCell, so I could not use the method copyTo.
Is there a way to add a cell / sheet from w1to a w2book?
edit2:
So should I create a writable copy of the book in another file?
( edit3: Or is there another free library that can do this?)
Update:
When I run this code, I get jxl.common.AssertionFailedexceptions in the line
WritableCellFormat newFormat = new WritableCellFormat(readFormat);
If I delete this line and change the code to
newCell.setCellFormat(readFormat);
then cell styles are not copied (fonts, cell borders, etc.).
try {
Workbook sourceDocument = Workbook.getWorkbook(new File("C:\\source.xls"));
WritableWorkbook writableTempSource = Workbook.createWorkbook(new File("C:\\temp.xls"), sourceDocument);
WritableWorkbook copyDocument = Workbook.createWorkbook(new File("C:\\copy.xls"));
WritableSheet sourceSheet = writableTempSource.getSheet(0);
WritableSheet targetSheet = copyDocument.createSheet("sheet 1", 0);
for (int row = 0; row < sourceSheet.getRows(); row++) {
for (int col = 0; col < sourceSheet.getColumns(); col++) {
WritableCell readCell = sourceSheet.getWritableCell(col, row);
WritableCell newCell = readCell.copyTo(col, row);
CellFormat readFormat = readCell.getCellFormat();
WritableCellFormat newFormat = new WritableCellFormat(readFormat);
newCell.setCellFormat(newFormat);
targetSheet.addCell(newCell);
}
}
copyDocument.write();
copyDocument.close();
writableTempSource.close();
sourceDocument.close();
} catch (Exception e) {
e.printStackTrace();
}
How can I copy cell styles to a new cell?