xlsx close Stream xlsx writing to zip stream encounters stream close

Write a function today: batch excel, parse it, and then fuzzy match with the data in the library. Since the manager requires that the data not fall to the ground, the whole process is written in the stream.

In the parsing part, whether it is xls or xlxs format, there is no problem, but when xlsx is written into the zip stream, the following problems will be encountered: the zip stream is actually closed.

 code show as below:


 The main idea is similar to the online one: create a zip stream, create an entry in the zip stream, write the workbook into the zip stream, and then close the entry. loop, closing the zip stream at the end.

Then, I forced to try not to close entry if it is in xlsx format;


 Then there is no more "stream closed" error.

but! ! ! Although the output downloaded file is zip, it has the same effect as ordinary xlsx decompression. (tip: xlsx is zip)

Finally, I searched a lot of places and couldn't find it, and then found it in a post on stackoverflow

https://stackoverflow.com/questions/16721010/write-a-xssfworkbook-to-a-zip-file?answertab=votes#tab-top

It is found that when writing the stream, xlsx itself is zip, which hijacks the zip stream, so the stream is closed (in fact, I don't understand).

In the end, the solution is: create another stream, write the workbook to the new stream, and then write the new stream to the zip stream. Then close the entry normally;


 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326255507&siteId=291194637