I would like to copy a sheet from an existing XLS document to a new one to a new location.
How could I do this with JXL?
Workbook w1 = Workbook.getWork
Beware of warning 'Maximum number of format records exceeded. Using default format.' Try use sth like Map to controll number of WritableCellFormat instances.
public static void createSheetCopy(WritableWorkbook workbook, int from, int to, String sheetName) throws WriteException {
WritableSheet sheet = workbook.getSheet(from);
WritableSheet newSheet = workbook.createSheet(sheetName, to);
// Avoid warning "Maximum number of format records exceeded. Using default format."
Map definedFormats = new HashMap();
for (int colIdx = 0; colIdx < sheet.getColumns(); colIdx++) {
newSheet.setColumnView(colIdx, sheet.getColumnView(colIdx));
for (int rowIdx = 0; rowIdx < sheet.getRows(); rowIdx++) {
if (colIdx == 0) {
newSheet.setRowView(rowIdx, sheet.getRowView(rowIdx));
}
WritableCell readCell = sheet.getWritableCell(colIdx, rowIdx);
WritableCell newCell = readCell.copyTo(colIdx, rowIdx);
CellFormat readFormat = readCell.getCellFormat();
if (readFormat != null) {
if (!definedFormats.containsKey(readFormat)) {
definedFormats.put(readFormat, new WritableCellFormat(readFormat));
}
newCell.setCellFormat(definedFormats.get(readFormat));
}
newSheet.addCell(newCell);
}
}
}