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 spreadsheetThe 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 :
Colin Hebert
2010-10-10 13:06:43