How to copy one entire Excel sheet into another Excel sheet of the same workbook, using Java SE and Apache POI?
Asked
Active
Viewed 4.0k times
15
-
2http://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 Answers
24
You'll probably want the cloneSheet(sheetNumber) method on the Workbook. See the JavaDocs for details

Gagravarr
- 47,320
- 10
- 111
- 156
-
Thanks! How do I add this returned HSSFSheet object to the workbook? – Shumon Saha Aug 09 '11 at 18:37
-
1You don't have to, it's automatically added for you before it's returned to you – Gagravarr Aug 09 '11 at 21:56
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
-
Thanks! How do I add this returned HSSFSheet object to the workbook? – Shumon Saha Aug 09 '11 at 18:36
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