Java 删除工作表后打开excel时出现POI错误
我需要从工作簿中删除第一张工作表(工作表数>1) 这是我的代码:Java 删除工作表后打开excel时出现POI错误,java,excel,apache-poi,Java,Excel,Apache Poi,我需要从工作簿中删除第一张工作表(工作表数>1) 这是我的代码: inputStreamExcel = new FileInputStream(path); wrkbook = new XSSFWorkbook(inputStreamExcel ); wrkbook .removeSheetAt(0); outputStreamExcel = new FileOutputStream(path); wrkbook.write(outputStreamExcel );
inputStreamExcel = new FileInputStream(path);
wrkbook = new XSSFWorkbook(inputStreamExcel );
wrkbook .removeSheetAt(0);
outputStreamExcel = new FileOutputStream(path);
wrkbook.write(outputStreamExcel );
outputStreamExcel .flush();
outputStreamExcel .close();
我没有任何例外。
但是,当我尝试打开excel文件时,出现了一个错误:
excel发现无法读取的内容
如果excel未正确保存,则会发生此错误,
您可能希望检查保存时,
当前选定/活动单元格
是否未指向空行,或者活动工作表
是否不为空请在删除第一张工作表后再次尝试设置活动工作表:
...
wrkbook.removeSheetAt(0);
wrkbook.setActiveSheet(0);
...
这应该可以解决问题。您所说的活动单元格是什么意思?活动单元格是指当前选定的单元格。此外,在删除当前工作表之前/之后(无论如何),需要更改活动工作表。我认为它解决了问题,在接受此答案之前,我将运行更多测试场景。谢谢