35

is there any way to update existing sheet name of MS Excel file knowing that I am using apache poi in my android app

I can create a sheet with my custom name

HSSFSheet sheet = workbook.createSheet("my custom name");

but when I want to copy another sheet to this one , the name also is copied and my custom name is crashed

funfordevelopping
  • 383
  • 1
  • 3
  • 7

3 Answers3

78

The following should do the trick:

workbook.setSheetName(workbook.getSheetIndex(sheet), "newName");
Omri Spector
  • 2,431
  • 24
  • 22
nikis
  • 11,166
  • 2
  • 35
  • 45
  • I am getting the following error while using setsheetName method in Apache POI. java.lang.IllegalArgumentException: Sheet index (-1) is out of range (0..3) Can anyone help? – JavaDragon Feb 13 '19 at 07:35
4

If you already know the sheet index, simply call

workbook.setSheetName(sheet-index, "my sheet name");

where sheet-index is the sheet number (0 based)

Jonathan L
  • 9,552
  • 4
  • 49
  • 38
0

Sometimes, while using setSheetName, I get IllegalArgumentException that : Sheet index (-1) is out of range (0..0) even when I have just 1 sheet. So, I just did this and it worked.

    workbook.getCTWorkbook().getSheets().getSheetArray(indexOfSheet).setName(sheetName);