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

amd*_*dev 3 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);
    }
Run Code Online (Sandbox Code Playgroud)

错误已开启 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)
Run Code Online (Sandbox Code Playgroud)

任何的想法 ?

我在http://apache-poi.1045710.n5.nabble.com/Unexpected-missing-row-when-some-rows-already-present-td5527417.html上看过这个问题,但他们没有给出好答案

我使用POI 3.12

更新:我的excel包含53行和数百列,所以他不是空的.将Excel与生成商务大对象一些人有同样的问题,没有解决方案.

HSSFSheet的源代码在这里

amd*_*dev 5

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

升级到3.14解决问题.

3.13和之前的例外由此代码生成

  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                     }
Run Code Online (Sandbox Code Playgroud)

但在3.14 他们只是评论它

    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);
    }
Run Code Online (Sandbox Code Playgroud)

正如他们所说,有些工具会跳过RowRec,在我的情况下,当我用Buisness对象生成我的.xls时