Excel POI某些行已存在时意外丢失的行

时间:2016-06-14 08:16:54

标签: java excel apache-poi

有人已经看到了这个错误吗?

我在创建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是使用商务对象some people have the very same issues生成的,没有解决方案。

HSSFSheet的源代码是here

2 个答案:

答案 0 :(得分:5)

好的,我找到了解决方案。

升级到3.14解决问题。

3.13及之前的例外情况由this code

生成
<credentials>

但是在3.14 they just comment it

  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                     }

正如他们所说,有些工具会跳过RowRec,在我的情况下,当我使用商务对象

生成我的.xls时

答案 1 :(得分:0)

示例:

Workbook workbook = null;
try {
    workbook = WorkbookFactory.create(is);

} catch (IOException e) {
    e.printStackTrace();

} catch (InvalidFormatException e) {
    e.printStackTrace();
}
Sheet sheet = workbook.getSheetAt(0);

XSSF或HSSF并不重要,两者都应该运作良好。