Search code examples
javaapache-poiexport-to-exceltemporary-files

How to get the size in bytes of an excel file while writing to it in Apache POI?


I have a bit of a problem around here, I just can't get it done.

The thing is that I'm using POI for a project in Java and I have to get the final outputs in .xls format(HSSF for Apache).

So, my business rules state that each file I generate has to be 12 MB as a maximun file size.

But I know .xls has some internal way of dealing the data(XML I guess), so this adds more bytes than just putting the result in a plain text file. I just can't get the size of the Excel Workbook since it generates itself in a temporal location(I can't find it) and I just can't read it while writting.

Is there any way to get the size in bytes of the Excel output file while Java writes to it using the HSSF Workbook Object?


Solution

  • Well, after some research on the API, I found out that the method called getBytes() returns an Array of bytes of every data on the workbook (Sheets, rows, data, etc) so using the lenght would return a very close aproximate of the bytes generated by the final workbook used by the user.