gpt4 book ai didi

java - 无法使用 HSSF Java 删除 Excel 工作表

转载 作者:太空宇宙 更新时间:2023-11-04 09:48:21 24 4
gpt4 key购买 nike

我无法从工作簿中删除 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)

最佳答案

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();

关于java - 无法使用 HSSF Java 删除 Excel 工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55136373/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com