2012-12-14 19 views
8

Sto provando a eseguire i miei primi test di lettura di file xlsx di grandi dimensioni con POI, ma per eseguire un semplice test con un file piccolo non riesco a mostrare il valore di una cella.Leggere il file xlsx con il POI (SXSSFWorkbook)

Qualcuno può dirmi qual è il mio errore. Tutti i suggerimenti sono ben accetti Grazie.

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 
    } 
} 

Risultato:

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

test.xlsx:

enter image description here

risposta

21

Consultare: similar question SXSSFWorkBook è solo in scrittura, non supporta la lettura.

Per la lettura poca memoria di file .xlsx, si dovrebbe guardare al XSSF and SAX EventModel documentation: Gagravarr

Se la memoria non sarebbe un problema si potrebbe usare un XSSFSheet invece per esempio

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()); 
+0

Grazie, ma ho bisogno di SXSSFWorkbook viene utilizzato per elaborare un file di grandi dimensioni senza eliminare l'errore di memoria. – alditis

+1

Si prega di consultare: [stessa domanda] (http://stackoverflow.com/questions/12513981/reading-data-from-xlsx-sxssfsheet-with-apache-poi-java) SXSSFWorkBook è solo scrittura, non supporta la lettura – IDKFA

+0

Usando SXSSFWorkbook possiamo evitare di buttare fuori dall'eccezione di memoria. E possiamo scrivere più di un record di llakh. Ma permette solo il formato .xlsx. – swamy

-1

troppo ho affrontato lo stesso problema di OOM durante l'analisi di file xlsx ... dopo due giorni di lotta, ho finalmente scoperto il codice qui sotto che è stato davvero perfetto;

Questo codice è basato su sjxlsx. Legge xlsx e memorizza in un foglio HSSF.

  // 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; 
+0

Come funziona questo aiuto per i file XSSF? Il tuo codice è solo HSSF ... – Gagravarr

Problemi correlati