Apache POI xlsx “We found a problem with some content in *.xlsx. Do you want us to try to recover it as much as we can” error due to XML parsing error

I am generating an .xlsx file using Apache POI 5.2.5, sending the byte array to the client side and downloading the file locally on the client’s machine.

When trying to open the file, I’m getting “We found a problem with some content in *.xlsx. Do you want us to try to recover it as much as we can” error. Clicking yes the file opens both on Windows and on Mac and all the data is present, but not as a table object. Trying to open the file using google sheets works without issues and the table object has a border.

The issue persisted also when I tried to save it directly on the server’s machine, so I don’t think the problem is on the client side.

The code generating the byte array:

private byte[] generateExcelBytes(List<DATA_OBJECT> data) {
    try (XSSFWorkbook workbook = new XSSFWorkbook()) {
        XSSFSheet sheet = workbook.createSheet("Data");

        CellStyle headerStyle = workbook.createCellStyle();
        Font headerFont = workbook.createFont();
        headerFont.setBold(true);
        headerFont.setFontHeightInPoints((short) 14);
        headerStyle.setFont(headerFont);

        CellStyle dataStyle = workbook.createCellStyle();
        Font dataFont = workbook.createFont();
        dataFont.setFontHeightInPoints((short) 12);
        dataStyle.setFont(dataFont);

        createTable(sheet, 0, 0, headerStyle, dataStyle, data);

        // Write the workbook content to a ByteArrayOutputStream
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        return outputStream.toByteArray();
    } catch (IOException e) {
        logger.error("Exception when creating xlsx file for account {} CDI status tables", account.getId(), e);
    }
    return null;
}

private void createTable(XSSFSheet sheet, int startRow, int startCol, CellStyle headerStyle, CellStyle dataStyle, List<DATA_OBJECT> data) {
    // Create sample data (replace with your actual data generation logic)
    Row headerRow = sheet.createRow(startRow);

    // for each header
    createCell(headerRow, startCol + X, HEADER_NAME, headerStyle);

    // Create data rows
    for (int i = 0; i < data.size(); i++) {
        DATA_OBJECT data_object = data.get(i);
        Row dataRow = sheet.createRow(startRow + i + 1);
        // for each data object
        createCell(dataRow, startCol, data_object.get_relevant_value, dataStyle);
    }

    int endRow = startRow + baseHoldings.size();
    int endCol = startCol + NUM_OF_COLUMNS;

    XSSFAutoFilter autoFilter = sheet.setAutoFilter(new CellRangeAddress(startRow, endRow, startCol, endCol));
    for (int i = startCol; i <= startCol + NUM_OF_COLUMNS; i++) {
        sheet.autoSizeColumn(i);
        sheet.setColumnWidth(i, sheet.getColumnWidth(i) + 1000);
    }

    // Create Excel table
    XSSFTable table = sheet.createTable(null);

    CTTable cttable = table.getCTTable();
    cttable.setDisplayName("Data Table");
    cttable.setId(1);
    cttable.setName("Data_Table");
    cttable.setRef(new CellRangeAddress(startRow, endRow, startCol, endCol).formatAsString());
}

private void createCell(Row row, int columnIndex, String value, CellStyle style) {
    Cell cell = row.createCell(columnIndex);
    cell.setCellValue(value);
    cell.setCellStyle(style);
}

Opening the file on Mac, I managed to see an actual error:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
    <logFileName>Repair Result to <FILE_NAME>0.xml</logFileName>
    <summary>Errors were detected in file '<FILE_PATH>.xlsx'</summary>
    <removedParts summary="Following is a list of removed parts:">
        <removedPart>Removed Part: /xl/tables/table1.xml part with XML error.  (Table) Xml parsing error Line 2, column 70.</removedPart>
    </removedParts>
</recoveryLog>

/xl/tables/table1.xml file contents:

<?xml version="1.0" encoding="UTF-8"?>
<table id="1" displayName="Data Table" name="Data_Table" ref="A1:H3" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"/>

I don’t know what causes the XML parsing error, or how to avoid it when generating the file on the server side.
Any help will be appreciated.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật