3
votes

Someone have already seen this error ?

I get it just when I create my 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);
    }

error is on 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)

Any idea ?

I have seen this question on http://apache-poi.1045710.n5.nabble.com/Unexpected-missing-row-when-some-rows-already-present-td5527417.html but they don't give good answer

I use POI 3.12

UPDATE : My excel contain 53 rows and hundreds columns so he is not empty. The excel was generated with Buisness Object and some people have the very same issues with no solution.

Source code of HSSFSheet is here

2

2 Answers

5
votes

Ok I've found the solution.

Upgrade to 3.14 solve the problem.

The exception in 3.13 and previous is generated by 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                     }

But on 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);
    }

As they said, some tools skip the RowRec, in my case it's when i generate my .xls with Buisness Object

0
votes

example:

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

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

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

XSSF or HSSF doesn't matter, both should work well.