14

I'm trying to do my first tests of reading large xlsx file with POI, but to do a simple test with a small file I fail to show the value of a cell.

Someone can tell me what is my mistake. All the suggestions are welcome. Thanks.

Test.java:

import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Test {

    public static void main(String[] args) throws Throwable {
        File file = new File("/tmp/test.xlsx");
        OPCPackage pkg = OPCPackage.open(new FileInputStream(file.getAbsolutePath()));
        XSSFWorkbook xssfwb = new XSSFWorkbook(pkg);

        SXSSFWorkbook wb = new SXSSFWorkbook(xssfwb, 100);
        Sheet sh = wb.getSheet("Hola");

        System.out.println("Name: "+sh.getSheetName()); // Line 19
        System.out.println("Val: "+sh.getRow(1).getCell(1).getStringCellValue()); // Line 20
    }
}

Result:

Name: Hola
Exception in thread "main" java.lang.NullPointerException
    at Test.main(Test.java:20)

test.xlsx:

enter image description here

alditis
  • 4,633
  • 3
  • 49
  • 76

2 Answers2

29

Please consult: similar question SXSSFWorkBook is write only, it doesn't support reading.

For low memory reading of .xlsx files, you should look at the XSSF and SAX EventModel documentation : Gagravarr

If memory wouldn't be an issue you could use a XSSFSheet instead e.g.

    File file = new File("D:/temp/test.xlsx");
    FileInputStream fis = new FileInputStream(file);
    XSSFWorkbook wb = new XSSFWorkbook(fis);

    XSSFSheet sh = wb.getSheet("Hola");
    System.out.println(sh.getLastRowNum());
    System.out.println("Name: "+sh.getSheetName()); 
    Row row = sh.getRow(1);

    System.out.println(row.getRowNum());

    System.out.println("Val: "+sh.getRow(1).getCell(1).getStringCellValue()); 
Community
  • 1
  • 1
IDKFA
  • 436
  • 3
  • 5
  • Thanks, but I need SXSSFWorkbook is used to process a large file without throwing out memory error. – alditis Dec 14 '12 at 07:24
  • 1
    Please consult: [same question](http://stackoverflow.com/questions/12513981/reading-data-from-xlsx-sxssfsheet-with-apache-poi-java) SXSSFWorkBook is write only, it doesn't support reading – IDKFA Dec 14 '12 at 08:09
  • By using SXSSFWorkbook we can avoid throwing out of memory exception.And we can write more than llakh records.But it allows only .xlsx format. – swamy Dec 14 '12 at 12:34
-1

I too faced the same issue of OOM while parsing xlsx file...after two days of struggle, I finally found out the below code that was really perfect;

This code is based on sjxlsx. It reads the xlsx and stores in a HSSF sheet.

            // read the xlsx file
       SimpleXLSXWorkbook = new SimpleXLSXWorkbook(new File("C:/test.xlsx"));

        HSSFWorkbook hsfWorkbook = new HSSFWorkbook();

        org.apache.poi.ss.usermodel.Sheet hsfSheet = hsfWorkbook.createSheet();

        Sheet sheetToRead = workbook.getSheet(0, false);

        SheetRowReader reader = sheetToRead.newReader();
        Cell[] row;
        int rowPos = 0;
        while ((row = reader.readRow()) != null) {
            org.apache.poi.ss.usermodel.Row hfsRow = hsfSheet.createRow(rowPos);
            int cellPos = 0;
            for (Cell cell : row) {
                if(cell != null){
                    org.apache.poi.ss.usermodel.Cell hfsCell = hfsRow.createCell(cellPos);
                    hfsCell.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING);
                    hfsCell.setCellValue(cell.getValue());
                }
                cellPos++;
            }
            rowPos++;
        }
        return hsfSheet;