Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use POI SXSSF to read a large spreadsheet

Tags:

java

file

xls

I am trying to read an xls file by using SXSSF. I have read about SXSSF, but do not understandexactly how to use it. So I am running into some problems.

Can anybody help me with the java code for reading large xls files (some 100,000 rows and 7-8 sheets).

(Edit from the comments)

Here is what I have tried:

Workbook workBook = new SXSSFWorkbook(200); 
workBook = WorkbookFactory.create(inputStream); 
Sheet sheet = workBook.getSheetAt(0); 
int totalRows = sheet.getPhysicalNumberOfRows(); 

for (int i=0; i<totalRows; i++) { 
    Row row = sheet.getRow(i); 
    int totalCols = row.getPhysicalNumberOfCells(); 
    for(int j=0; j<totalCols; j++) { 
        Cell cell = row.getCell(j); 
    } 
 } 
like image 439
abhi Avatar asked Apr 07 '12 09:04

abhi


2 Answers

SXSSF is only to write large excel files (xlsx) and not to read.

To read large excel files, please refer to SAX parsing based event handling API of Apache POI at: https://poi.apache.org/components/spreadsheet/how-to.html.

A very good working example is present at: https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/examples/xssf/eventusermodel/XLSX2CSV.java

like image 91
vijayinani Avatar answered Sep 27 '22 18:09

vijayinani


SXSSF (since 3.8-beta3) – is an API-compatible streaming extension of XSSF to be used when very large spreadsheets have to be produced

As you can see, it is clearly specified SXSSF is used when large spreadsheets are to be produced. You can use XSSF. It is similar and also is good enough.

like image 23
Chetan Oswal Avatar answered Sep 27 '22 17:09

Chetan Oswal