I notice that the setSheetHidden method of the org.apache.poi.hssf.model.InternalWorkbook class does not check its sheetnum parameter: public void setSheetHidden(int sheetnum, SheetVisibility visibility) { BoundSheetRecord bsr = getBoundSheetRec(sheetnum); bsr.setHidden(visibility == SheetVisibility.HIDDEN); bsr.setVeryHidden(visibility == SheetVisibility.VERY_HIDDEN); } The other method, setSheetName, does check the sheetnum parameter: public void setSheetName(int sheetnum, final String sheetname) { checkSheets(sheetnum); // YK: Mimic Excel and silently truncate sheet names longer than 31 characters String sn = (sheetname.length() > 31) ? sheetname.substring(0, 31) : sheetname; BoundSheetRecord sheet = boundsheets.get(sheetnum); sheet.setSheetname(sn); } private void checkSheets(int sheetnum) { if ((boundsheets.size()) <= sheetnum) { // if we're short one add another.. if ((boundsheets.size() + 1) <= sheetnum) { throw new RuntimeException("Sheet number out of bounds!"); } BoundSheetRecord bsr = createBoundSheet(sheetnum); records.add(records.getBspos()+1, bsr); records.setBspos( records.getBspos() + 1 ); boundsheets.add(bsr); getOrCreateLinkTable().checkExternSheet(sheetnum); fixTabIdRecord(); } } Please add the check to the setSheetHidden method.
Applied via r1849717, however HSSFWorksheet applies more strict checks anyway, so the "add a record with +1" is never actually used in normal code usage.