Jake
Jake

Reputation: 610

After removing a sheet from an excel workbook with Apache Poi, a sheet remains?

I'm using workbook.removeSheetAt() to remove a certain named sheet from a workbook. However, the workbook contains a new sheet called "Sheet3" after writing it out to disk. What gives? I'm not creating this new sheet anywhere in my code. Note: I am using XSSFWorkbook and all the other XSSF stuff.

Some more info: Here is a snippet from workbook.xml inside the output xlsx. I am deleting sheet with index=2, ostensibly sheetId=3. The template workbook I am starting with only has four sheets.

<sheets>
    <sheet r:id="rId1" sheetId="1" name="Sheet1"/>
    <sheet r:id="rId2" sheetId="2" name="Params" state="hidden"/>
    <sheet r:id="rId4" sheetId="4" name="Warnings" state="hidden"/>
    <sheet r:id="rId3" sheetId="5" name="Sheet3"/>
</sheets>

Upvotes: 1

Views: 3714

Answers (1)

John B
John B

Reputation: 32949

Apache POI is using index based sheets (kinda like an array). If you have a sheet at a position that is greater than x, there must exist a sheet at position x even if it is blank. You could try moving the sheets where n > x to position n - 1 using setSheetOrder

Seems like there is a bug report similar to this: Bug Report

What about hiding the sheet: setSheetHidden

Upvotes: 2

Related Questions