Error While Reading Large Excel Files (xlsx) Via Apache POI

后端 未结 4 1649
旧巷少年郎
旧巷少年郎 2020-11-29 07:31

I am trying to read large excel files xlsx via Apache POI, say 40-50 MB. I am getting out of memory exception. The current heap memory is 3GB.

I can read smaller exc

4条回答
  •  借酒劲吻你
    2020-11-29 08:17

    You don't mention whether you need to modify the spreadsheet or not.

    This may be obvious, but if you don't need to modify the spreadsheet, then you don't need to parse it and write it back out, you can simply read bytes from the file, and write out bytes, as you would with, say an image, or any other binary format.

    If you do need to modify the spreadsheet before sending it to the user, then to my knowledge, you may have to take a different approach.

    Every library that I'm aware of for reading Excel files in Java reads the whole spreadsheet into memory, so you'd have to have 50MB of memory available for every spreadsheet that could possibly be concurrently processed. This involves, as others have pointed out, adjusting the heap available to the VM.

    If you need to process a large number of spreadsheets concurrently, and can't allocate enough memory, consider using a format that can be streamed, instead of read all at once into memory. CSV format can be opened by Excel, and I've had good results in the past by setting the content-type to application/vnd.ms-excel, setting the attachment filename to something ending in ".xls", but actually returning CSV content. I haven't tried this in a couple of years, so YMMV.

提交回复
热议问题