1

我无法从工作簿中删除 excel 工作表。我尝试了不同的解决方案,但无法找到解决方法。假设我想使用“wb.RemoveSheetAt(5)”删除第 5 号工作表,我只能在删除第 6 号工作表后才能删除它。

代码 :

public void cloneSheet(String sheetName , int NoOfClones){
    int temp = 0;
    for(int i = 0 ; i <= NoOfClones ; i++){
        HSSFSheet tsheet = workbook.cloneSheet(temp = workbook.getSheetIndex(sheetName));
        workbook.setSheetName((workbook.getSheetIndex(tsheet)), sheetName+i);   
    }
    workbook.removeSheetAt(workbook.getSheetIndex(sheetName));
}

这是我得到的错误:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 21, Size: 21
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at org.apache.poi.hssf.record.ExternSheetRecord.getRef(ExternSheetRecord.java:164)
at org.apache.poi.hssf.record.ExternSheetRecord.adjustIndex(ExternSheetRecord.java:168)
at org.apache.poi.hssf.model.LinkTable.updateIndexToInternalSheet(LinkTable.java:419)
at org.apache.poi.hssf.model.InternalWorkbook.removeSheet(InternalWorkbook.java:721)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.removeSheetAt(HSSFWorkbook.java:899)
at exceleditor.ExcelEditor.cloneSheet(ExcelEditor.java:104)
at exceleditor.QLaunchExcelEditor.main(QLaunchExcelEditor.java:290)
4

1 回答 1

0
FileInputStream fileStream = new FileInputStream(excelFile);
POIFSFileSystem poiFS = new POIFSFileSystem(fileStream);
HSSFWorkbook workbook = new HSSFWorkbook(poiFS);
workbook.removeSheetAt(5);
FileOutputStream output = new FileOutputStream(excelFile);
workbook.write(output);
output.close();
于 2019-03-13T07:34:59.877 回答