3

我在使用 XSSF 的 apache poi for excel 2007 时遇到问题

我在下面有两个代码片段(胖手指)。第一个似乎有效,因为工作簿在 excel 2007 中正确打开,并且有一些变化。

如果我运行第二个代码片段然后在 excel 2007 中打开,我会收到有关不可读内容的错误。我必须单击一个确认框,然后 excel 会打开我的文件。

为什么第二个代码片段会导致此错误?唯一的区别是第二个我试图将所有行从第 2 行(零索引)向上移动两行......

我真正想做的就是删除一些标题行,然后从文件中删除一些页脚行。我没有做任何花哨的事情。我猜我误解了 API,但我已经为此奋斗了一段时间。

有任何想法吗?我还应该移动行,还是删除它们,或者两者兼而有之?

String filename = "C:\\file.xlsx";
FileInputStream file = new FileInputStream( new File(fileName) );

XSSFWorkbook wb = new XSSFWorkbook(file);

XSSFSheet sheet = wb.getSheetAt(0);
int lastRow = sheet.getlastRow();

sheet.shiftRows(1, lastRow, -1);


file.close()
FileOutputStream out = new FileOutputStream(fileName);
wb.write(out);
out.flush();
out.close();

String filename = "C:\\file.xlsx";
FileInputStream file = new FileInputStream( new File(fileName) );

XSSFWorkbook wb = new XSSFWorkbook(file);

XSSFSheet sheet = wb.getSheetAt(0);
int lastRow = sheet.getlastRow();

sheet.shiftRows(2, lastRow, -2);


file.close()
FileOutputStream out = new FileOutputStream(fileName);
wb.write(out);
out.flush();
out.close();

编辑:实际上,第一个片段也引起了问题……有时????我的印象是我做错了什么......有什么建议吗?

4

1 回答 1

0

I have run into a similar problem. I think that the problem might lie with the handling of comments. In my case, the excel file was reported as "damaged" by Excel whenever a shift was done when comments were present. Excel repaired the file and reported that it deleted the comments part and the rest was fine. Excel files without comments but with the same processing code, did not cause any problems.

Sorry that this does not solve the problem, but maybe it provides for a workaround. In my case, I delete all the comments programmatically (ugly).

于 2015-02-18T11:11:20.153 回答