Copy sheet with JXL in Java

后端 未结 6 1653
轮回少年
轮回少年 2021-01-03 05:21

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         


        
6条回答
  •  失恋的感觉
    2021-01-03 06:18

    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 :

    • JExcel API Frequently Asked Questions

提交回复
热议问题