3

I'm attempting to create a very basic spreadsheet (XLS) viewer for android. While using the Apache poi API I'm getting out of memory errors when creating a new XSSFWorkbook object:

mWorkBook = new XSSFWorkbook(file);

How can grab specific rows/cells inside the .xls without loading the entire file into memory?

Habbert
  • 221
  • 1
  • 10
  • How can grab specific rows/cells? I think that answer Just reads the entire file. – Habbert Feb 05 '16 at 15:37
  • It reads the entire file, but it does so without loading the entire thing into memory, and you can close the stream without reading all the rows. I.e. once you've read the desired row you can close it. Check out the streaming API in Apache POI. http://poi.apache.org/spreadsheet/index.html – Samuel Feb 05 '16 at 15:52
  • But can it start reading somewhere else other than the first row? What if I need a row half way through the file? reading the entire file up to that point to get that row wont work well. – Habbert Feb 05 '16 at 15:55
  • 1
    Spreadsheets aren't designed to be accessed that way. You have to deal with them within the limitations of the file format. If you need quick random read access, I would recommend loading the data into an indexed store (like a database). – Samuel Feb 05 '16 at 16:01

1 Answers1

1

Spreadsheets aren't ideal for random access. The reason why spreadsheets aren't designed for this purposed is because cells can be arbitrarily long, and rows can have an arbitrary number of cells in them. If this were not the case and each row had a fixed size in bytes, then you could simply skip reading the first (ROWSIZE * INDEXOFROW) bytes of the file.

If random access is your primary use case I would recommend loading the data into a small database. Take a look at H2.

Samuel
  • 16,923
  • 6
  • 62
  • 75