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
How can I copy a worksheet in one workbook to a new worksheet in another workbook?
This can be done, but requires a little work. Firstly, you have to copy it cell (within a couple of nested for loops). For each cell you need to invoke the copyTo()
method, which will produce a deep copy. However the format is only shallow copied, so you will need to get the cell format and use the copy constructor of that, and then call setCellFormat on the cell you have just copied. Then add the duplicate cell to the new spreadsheet
The code might look as follows:
for (int i = 0 ; i < numrows ; i++){
for (int j = 0 ; j < numcols ; j++){
readCell = sheet.getCell(i, j);
newCell = readCell.copyTo(i, j);
readFormat = readCell.getCellFormat();
newFormat = new WritableCellFormat(readFormat);
newCell.setCellFormat(newFormat);
newSheet.add(newCell);
}
}
Resources :