Excel – Opening File Created with SXSSFWorkbook in Excel: “Unreadable Content Found”

apache-poiexcelopenxml

There are several related questions, but I can't find one that reflects my situation.

I'm writing out an Excel "xlsx" file with Apache POI using the SXSSFWorkbook and SXSSFSheet objects. The file is created without issue and opens fine in LibreOffice, however, Excel complains when opening the file.

Excel found unreadable content in 'test-file.xlsx'. Do you want to recover the contents of the workbook? If you trust the source of this workbook, click Yes.

When "Yes" is selected…

Excel was able to open the file by repairing or removing the unreadable content.

Removed Feature: Format from /xl/styles.xml part (Styles)

Repaired Records: Cell information from /xl/worksheets/sheet1.xml part

The code to create this workbook is pretty boring, I don't set up any styles or anything interesting. I create the workbook and one sheet, then write data to it.

private Workbook createWorkbook(final String sheetName, final String[] headers) {

    // create a new workbook and sheet
    final SXSSFWorkbook workbook = new SXSSFWorkbook(500);
    final SXSSFSheet sheet = (SXSSFSheet) workbook.createSheet(sheetName);

    // create and fill our header row
    final Row row = sheet.createRow(0);
    for (int index = 0; index < headers.length; index++) {
        row.createCell(index).setCellValue(headers[index]);
    }

    return workbook;
}

Writing the data is equally uninteresting.

private void exportPersonWorkbook(final Workbook workbook, final String sheetName, final PersonExport personExport) {

    // list of data for this new row
    final List rowValues = new ArrayList();

    // person id
    rowValues.add(personExport.getContactId());

    // dec region number
    rowValues.add(personExport.getRegion());

    // birth date
    rowValues.add(personExport.getBirthDate());

    // day phone
    rowValues.add(personExport.getMailingContactInfoBusinessPhone());

    ...and so on...

    writeRowToWorkbook(workbook, sheetName, rowValues);
}

private void writeRowToWorkbook(final Workbook workbook, final String sheetName, final List values) {

    // helper and our date format
    final CreationHelper creationHelper = workbook.getCreationHelper();
    final CellStyle dateStyle = workbook.createCellStyle();
    dateStyle.setDataFormat(creationHelper.createDataFormat().getFormat(
            PesticidesDomainConstants.DEFAULT_DATE_PATTERN));

    // get a handle on our worksheet
    final Sheet sheet = workbook.getSheet(sheetName);

    // create our new row
    final Row row = sheet.createRow(sheet.getLastRowNum() + 1);

    // write all of our data to the row
    int column = 0;
    final Iterator iterator = values.iterator();
    while (iterator.hasNext()) {

        final Object value = iterator.next();
        if (value != null) {

            // create our new cell
            final Cell cell = row.createCell(column);

            // Excel cells can only handle certain data types
            if (value instanceof String) {
                cell.setCellValue((String) value);
            } else if (value instanceof Integer) {
                cell.setCellValue((Integer) value);
            } else if (value instanceof Double) {
                cell.setCellValue((Double) value);
            } else if (value instanceof Date) {
                cell.setCellValue((Date) value);

                // set the cell format for dates
                cell.setCellStyle(dateStyle);
            } else {

                // last ditch effort to populate cell
                cell.setCellValue(value.toString());
            }
        }

        // increment our column counter
        column++;
    }
}

The file looks nearly correct when opened, I can't see anything different. Has anyone seen this issue using Apache POI? I'm hoping for something simple, like an additional flag or setting to make this work. Any help would be greatly appreciated!

Best Answer

I resolved the issue and I am embarrassed that I didn't spot the issue earlier. If you look at the "writeRowToWorkbook" method, you'll see that it creates a CellStyle for cells that hold a date. Clearly, adding a CellStyle to the worksheet every time a row is written is not a great idea. Moving the creation of the date CellStyle out into the workbook method resolved this issue.

Related Topic