有人已经看到这个错误了吗?
我在创建 HSSFWorkbook 时就知道了
try {
LOGGER.info("Open Excel file: " + filename);
InputStream inputStream = new FileInputStream(filename);
Workbook wb = new HSSFWorkbook(inputStream);
Sheet sheet = wb.getSheetAt(0);
/* save excel */
FileOutputStream fileOut = new FileOutputStream(filenameOutput);
wb.write(fileOut);
fileOut.close();
wb.close();
inputStream.close();
} catch (IOException e1) {
LOGGER.log(Level.SEVERE, e1.getMessage(), e1);
}
错误出现在 new HSSFWorkbook(inputstream)
Exception in thread "main" java.lang.RuntimeException: Unexpected missing row when some rows already present
at org.apache.poi.hssf.usermodel.HSSFSheet.setPropertiesFromSheet(HSSFSheet.java:212)
at org.apache.poi.hssf.usermodel.HSSFSheet.<init>(HSSFSheet.java:137)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:338)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:289)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:224)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:382)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:364)
有什么想法吗?
我在 http://apache-poi.1045710.n5.nabble.com/Unexpected-missing-row-when-some-rows-already-present-td5527417.html 上看到过这个问题但他们没有给出好的答案
我用的是POI 3.12
更新:我的 excel 包含 53 行和数百列,所以他不是空的。 excel 是用 Buisness Object 和 some people have the very same issues 生成的没有解决方案。
HSSFSheet 的源代码是here
最佳答案
好的,我找到了解决方案。
升级到3.14解决问题。
3.13及之前的异常是由this code产生的
RowRecord row = sheet.getNextRow();
boolean rowRecordsAlreadyPresent = row != null;
207 if (hrow == null) {
208 // Some tools (like Perl module Spreadsheet::WriteExcel - bug 41187) skip the RowRecords
209 // Excel, OpenOffice.org and GoogleDocs are all OK with this, so POI should be too.
210 if (rowRecordsAlreadyPresent) {
211 // if at least one row record is present, all should be present.
212 throw new RuntimeException("Unexpected missing row when some rows already present");
213 }
但是在 3.14 they just comment it
if (hrow == null) {
/* we removed this check, see bug 47245 for the discussion around this
// Some tools (like Perl module Spreadsheet::WriteExcel - bug 41187) skip the RowRecords
// Excel, OpenOffice.org and GoogleDocs are all OK with this, so POI should be too.
if (rowRecordsAlreadyPresent) {
// if at least one row record is present, all should be present.
throw new RuntimeException("Unexpected missing row when some rows already present");
}*/
// create the row record on the fly now.
RowRecord rowRec = new RowRecord(cval.getRow());
sheet.addRow(rowRec);
hrow = createRowFromRecord(rowRec);
}
正如他们所说,一些工具会跳过 RowRec,在我的例子中,当我使用 Buisness 对象
生成我的 .xls 时关于java - 当某些行已经存在时,Excel POI 意外丢失行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37806547/