dhatim / fastexcel

Generate and read big Excel files quickly
Other
647 stars 116 forks source link

Corrupted file when generating excel #306

Closed inkman97 closed 10 months ago

inkman97 commented 1 year ago

Hi, We are using latest version of fastexcel in our project. It worked since Java 11 but now that we have migrated to Java 17 the excel file that is generated is corrupted. The strange behaviour is that if I generate the excel in local environment running on Java 17 the excel is correctly generated, but if I execute it in dev environment it gives the problem mentioned before. Only difference between the two is that local environment is on Windows machine and dev is on Linux.

Do you know what can be the issue?

Thanks in advance!

rzymek commented 1 year ago

The first thing I'd check is if you're not missing any close() method call (or use try-with-resources like in the sample)

inkman97 commented 1 year ago

Sure I'll check this first and will give an answer after. If this should be the case then how it's working fine locally and not on another env? Moreover it was working fine before in both envs, local and dev, but running with Java 11

rzymek commented 1 year ago

Windows and Linux have a totally different way of handling files. It might be working fine (by "accident") with a call to close() on Windows and fail on Linux. Or vice versa.

inkman97 commented 1 year ago

Windows and Linux have a totally different way of handling files. It might be working fine (by "accident") with a call to close() on Windows and fail on Linux. Or vice versa.

Do you think this can happen also between Java 11 vs 17?

rzymek commented 1 year ago

No really. I think the operating system makes the real difference here

inkman97 commented 1 year ago

No really. I think the operating system makes the real difference here

I checked on the code and we are creating workbook and related worksheet as stated on the readme file. Do you have other suggestions? Thanks!

inkman97 commented 1 year ago

No really. I think the operating system makes the real difference here

I checked the code and we are using CompletableFuture. I've checked another issue that is still opened in fastexcel project and it seems it's the same problem. What do you suggest?

rzymek commented 1 year ago

How are you using CompletableFuture? Fastexcel, as generally all io streams, is not thread-safe.

inkman97 commented 1 year ago

How are you using CompletableFuture? Fastexcel, as generally all io streams, is not thread-safe.

Yes for this reason I removed it and I structured the code in order to remove multi thread, but the error still persists. The situation I'm facing is the same mentioned into another open issue on fastexcel repo. We just create a workbook and from that workbook we set the values for each worksheet in that workbook. After this we call the finish() on workbook object and we close output stream. Do you have some suggestions?

kjit commented 10 months ago

Hi, I had similar issue, generated file was recognized as corrupted by Excel. In my case the problem was, that I've called workbook.finish() twice: one time explicit, second by autocloseable feature. So such piece of code generates invalid file:

 public static void main(String[] args) throws IOException {
        String name = "testexcel.xlsx";
        try (OutputStream os = new FileOutputStream(name); 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);
            wb.finish();
        }
    }

Once I removed wb.finish(), file is generated correctly. So I recommend to double check if this method is not called twice somehow. Env: Windows, Java 17

inkman97 commented 10 months ago

@kjit In my case the issue was related to timestamp because it was not compatible with java time using java 17. So I changed the timestamp and it worked