15

How to copy one entire Excel sheet into another Excel sheet of the same workbook, using Java SE and Apache POI?

Shumon Saha
  • 1,415
  • 6
  • 16
  • 33
  • 2
    http://stackoverflow.com/questions/5889639/apache-poi-copying-sheets – Ivan Sopov Aug 09 '11 at 07:52
  • 1
    @IvanSopov, thank you for the URL, but that question is how to copy sheets between different workbooks, whereas my question is how to copy sheets in the **same** workbook. – Shumon Saha May 13 '13 at 09:58

3 Answers3

24

You'll probably want the cloneSheet(sheetNumber) method on the Workbook. See the JavaDocs for details

Gagravarr
  • 47,320
  • 10
  • 111
  • 156
5

Did you check the API?

to copy a sheet into the same workbook, use HSSFWorkbook.clonesheet(int sheetIndex)

Ivan's comment has linked the question for copying across workbooks.

JoseK
  • 31,141
  • 14
  • 104
  • 131
4

Yes , this can be...Here my code.

            XSSFWorkbook workbook = new XSSFWorkbook(file);
            int totalRecords = 5;
            for (int i = 0; i < totalRecords - 1; i++) {
                workbook.cloneSheet(1);
            }
            for (int i = 1; i <= totalRecords; i++) {
                workbook.setSheetName(i, "S" + i);
            }
Cataclysm
  • 7,592
  • 21
  • 74
  • 123