Poi export excel file prompts "Because some content cannot be read, Excel cannot be opened"

First, describe the following phenomenon. The exported file can be opened normally using wps. Once the office is used, the above statement will be reported. You need to click the Open and Repair button. The error diagram is as follows:

Although the data was normal after the repair, the test was not allowed, which made me very depressed. I have written so many import and export, and the code is obviously not much different. Only this time there was an incompatibility problem. I found a lot of information. Now Summarizing some of the reasons and the final solution of the host, of course, the most stupid way is to comment out the code line by line and then export the test and find the final error code (but I tested this method n times and I did not find the error code...)
1. Whether the name of the sheet page contains time or special characters
2. The cell style setting is unreasonable, such as column width, row height
3. Content-Length should be configured in the controller layer response

response.setHeader("Content-Length", String.valueOf(arr.length)); // arr为字节数组

The landlord used the third method to solve it. I don’t know why. I commented the line again when I re-tested. After exporting the result, the office can be opened normally. This is the first time I feel the metaphysics of the code... Hereby Record it
Of course, it’s better to add Content-Type when configuring, as follows

response.setContentType("application/vnd.ms-excel");

Guess you like

Origin blog.csdn.net/m0_38001814/article/details/102505414