dhatim / fastexcel

Generate and read big Excel files quickly
Other
675 stars 122 forks source link

Creating an Excel-Sheet with two Worksheets results in a corrupted Excel-File (at least for MS Excel) #153

Closed methical closed 3 years ago

methical commented 3 years ago

I tried to create an Excel-File with two Worksheets. See this beautiful code as an example:

public class Excel {
    public static void main(String[] args) {
        try (OutputStream os = new FileOutputStream("test.xlsx")) {
            Workbook wb = new Workbook(os, "MyApplication", "1.0");
            Worksheet ws = wb.newWorksheet("Sheet 1");
            ws.value(0, 0, "This is a string in A1");
            ws.value(0, 1, new Date());
            ws.value(0, 2, 1234);
            ws.value(0, 3, 123456L);
            ws.value(0, 4, 1.234);

            Worksheet ws2 = wb.newWorksheet("Sheet 2");
            ws2.value(0, 0, "This is a string in A1");
            ws2.value(0, 1, new Date());
            ws2.value(0, 2, 1234);
            ws2.value(0, 3, 123456L);
            ws2.value(0, 4, 1.234);
            wb.finish();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

This creates a file which cannot be opened in Excel for Mac (16.47) or Excel 365 on Windows. It tells me the file is corrupt and no data can be recovered. However LibreOffice opens it without problems. Numbers works flawless too. Am I missing something here? I've seen user closing the worksheet, but this doesn't help. test.xlsx

I've attached corrupt Excel file for your convenience.

rzymek commented 3 years ago

I did confirm this problem in Excel Online.

I also got these errors when running docker run -it --entrypoint xlsx-validator vindvaki/xlsx-validator:

File: /target/test.xlsx
Error: The element has unexpected child element 'http://schemas.openxmlformats.org/spreadsheetml/2006/main:definedNames'.
ContentType: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml
XPath: /x:workbook[1]

File: /target/test.xlsx
Error: The attribute 'rgb' has invalid value '000000'. The actual length according to datatype 'hexBinary' is not equal to the specified length. The expected length is 4.
ContentType: application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml
XPath: /x:styleSheet[1]/x:fonts[1]/x:font[1]/x:color[1]

I'll try to fix those reported errors and recheck in Excel Online.