Overview Panoramica

There are two good choices for reading & writing Microsoft Excel Spreadsheet files from Java, in a platform independent way, - jexcelapi and Jakarta POI (HSSF) . Ci sono due buone scelte per la lettura e la scrittura Fogli di calcolo Microsoft Excel file da Java, in un modo indipendente dalla piattaforma, - jexcelapi e Jakarta POI (HSSF). Both of them provide nice interface to access Excel data structure and even generate new spreadsheet. Ciascuno di essi forniscono piacevole interfaccia per accedere a dati di Excel struttura e anche generare nuove foglio di calcolo. I have done extensive tests with both of them for a high-profile project for a Fortune 500 company. Ho fatto ampio test con ciascuno di essi per un alto profilo progetto per una società Fortune 500. Previously also I had successfully used HSSF for another high profile client. Anche in precedenza avevo usato con successo HSSF per un altro client di alto profilo. In the paragraphs below I present my conclusions and sample code for reading Excel spreadsheet from Java using both the libraries. Nei punti seguenti presentare conclusioni e il mio codice di esempio per la lettura di un foglio di calcolo Excel da Java utilizzando sia le librerie.

Comparison of JExcelAPI with Jakarta-POI (HSSF) Confronto delle JExcelAPI a Jakarta-POI (HSSF)

1. JExcelAPI is clearly not suitable for important data. evidentemente non è adatto per i dati importanti. It fails to read several files. Essa non riesce a leggere più file. Even when it reads it fails on cells for unknown reasons. Anche quando si legge che non riesce a cellule per motivi sconosciuti. In short JExcelAPI isn’t suitable for enterprise use. In breve JExcelAPI non è adatto per le imprese.

2. HSSF is the POI Project’s pure Java implementation of the Excel ‘97(-2002) file format. è il POI del Progetto pura implementazione Java di Excel'97 (-2002) formato di file. It is a mature product and was able to correctly and effortlessly read excel data generated from various sources, including non-MS Excel products like Open Office, and for various versions of Excel. Si tratta di un prodotto maturo ed è stato in grado di correttamente e senza fatica di leggere dati di Excel generati da varie fonti, compresi i non-MS Excel prodotti come Open Office, e per varie versioni di Excel. It is very robust and well featured. E 'molto robusto e ben caratterizzata. Highly recommended. Altamente consigliato.

3. Performance was never a consideration in our tests because a) data integrity is the single most important factor and b) there didn’t appear to be any significant performance difference while running the tests; both of them were very fast. Prestazioni non è mai stata una considerazione nei nostri test, perché a) l'integrità dei dati è il fattore più importante e b) non sembra essere in modo significativo le prestazioni differenza durante l'esecuzione delle prove; ciascuno di essi sono stati molto veloci. We didn’t bother to time it for the above reasons. Non abbiamo tempo a preoccuparsi per le ragioni di cui sopra.

How to read Excel Excel Spreadsheet from Java using Jakarta POI (HSSF) Come leggere un foglio di calcolo Excel Excel da Java utilizzando Jakarta POI (HSSF)

 try {     POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( file ));     HSSFWorkbook wb = new HSSFWorkbook(fs);     HSSFSheet sheet = wb.getSheetAt(0);     HSSFRow row;     HSSFCell cell;      int rows; // No of rows     rows = sheet.getPhysicalNumberOfRows();      int cols = 0; // No of columns     int tmp = 0;      // This trick ensures that we get the data properly even if it doesn’t start from first few rows     for(int i = 0; i < 10 || i < rows; i++) {         row = sheet.getRow(i);         if(row != null) {             tmp = sheet.getRow(i).getPhysicalNumberOfCells();             if(tmp > cols) cols = tmp;         }     }      for(int r = 0; r < rows; r++) {         row = sheet.getRow(r);         if(row != null) {             for(int c = 0; c < cols; c++) {                 cell = row.getCell((short)c);                 if(cell != null) {                     // Your code here                 }             }         }     } } catch(Exception ioe) {     ioe.printStackTrace(); } try (POIFSFileSystem fs = new POIFSFileSystem (nuovo FileInputStream (file)); HSSFWorkbook wb = new HSSFWorkbook (fs); HSSFSheet foglio = wb.getSheetAt (0); HSSFRow fila; HSSFCell cella; int righe; / / n. di righe righe = sheet.getPhysicalNumberOfRows (); int cols = 0; / / n. di colonne int tmp = 0; / / Questo trucco assicura che otteniamo i dati correttamente, anche se non si avvia da prime righe for (int i = 0; i <10 | | i <righe; i + +) (riga = sheet.getRow (i); if (riga! = null) (tmp = sheet.getRow (i). getPhysicalNumberOfCells (); if (tmp> cols) cols = tmp;)) for (int i = 0; r <righe; r + +) (riga = sheet.getRow (r); if (riga! = null) (for (int c = 0; c <cols; c + +) (cella Row.getCell = ((short) c); if (cella! = Null) (/ / Il codice qui))))) catch (Exception UIE) (ioe.printStackTrace ();) 

This sample should get you started. Questo campione deve iniziare. Don’t forget to import appropriately. Non dimenticare di importazione adeguato.

Gotchas while using Jakarta POI (HSSF) "Trucchi" durante l'utilizzo di Jakarta POI (HSSF)

  • getPhysicalNumberOfRows() returns the physical number of rows which may be more than the actual (logical) number of rows. getPhysicalNumberOfRows () restituisce il numero fisico di righe che possono essere superiori a quelli effettivi (logico) il numero di righe. The same goes for getPhysicalNumberOfCells(). Lo stesso vale per getPhysicalNumberOfCells ().
  • You should check for nulls when fetching the HSSFRow and HSSFCell objects as shown. Si dovrebbe verificare quando nulls per il recupero e la HSSFRow oggetti HSSFCell come mostrato in figura.
  • Remember that Excel tables are often sparsely populated. Ricorda che le tabelle di Excel sono spesso scarsamente popolate. So choose your data structures accordingly. Così scegliere il vostro strutture di dati di conseguenza.
  • POI accesses the data by sheet. POI accede al foglio di dati. In JExcelAPI you can directly access the data in any row and column. In JExcelAPI è possibile accedere direttamente i dati in ogni riga e colonna.

How to access Excel Spreadsheet using JExcelAPI Come accedere utilizzando un foglio di calcolo Excel JExcelAPI

 File fp = new File(file); try {     Workbook wb = Workbook.getWorkbook(fp);     Sheet sheet = wb.getSheet(0);     int columns = sheet.getColumns();     int rows = sheet.getRows();      String data;     for(int col = 0;col < columns;col++) {         for(int row = 0;row < rows;row++) {             data = sheet.getCell(col, row).getContents();             // Your code here         }     } } catch(Exception ioe) {     System.out.println("Error: " + ioe); } Fp = file nuovo file (file); try (di lavoro wb = Workbook.getWorkbook (fp); Foglio foglio = wb.getSheet (0); int colonne = sheet.getColumns (); int righe = sheet.getRows (); Stringa dati; for (int col = 0; col <colonne; col + +) (for (int riga = 0; riga <righe; riga + +) (dati = sheet.getCell (col, riga). getContents (); / / Il codice qui ))) Catch (Exception UIE) (System.out.println ( "Errore:" + UIE);) 

Gotchas while using JExcelAPI Durante l'utilizzo di trucchi JExcelAPI

  • JExcelAPI may often fail to fetch the data from certain cells or even the whole sheet. JExcelAPI possono spesso non riescono a recuperare i dati di alcune cellule o anche l'intero foglio. Unfortunately it gives a warning instead of an error to indicate the problem. Purtroppo dà un avvertimento invece di un errore di indicare il problema.
  • JExcelAPI doesn’t expose the full meta-data of the spreadsheet like POI does. JExcelAPI non esporre il pieno meta-dati del foglio di lavoro come fa POI.
  • JExcelAPI doesn’t properly recognize the data type in cells. JExcelAPI non riconoscere correttamente il tipo di dati nelle celle. In all cases it indicated String data in our tests even when there were numeric or date fields. In tutti i casi in essa indicato Stringa dati in nostro test, anche quando ci sono stati numerici o data campi.

Concluding thoughts on accessing Excel spreadsheets from Java La conclusione di pensieri sulle modalità di accesso al foglio di lavoro Excel da Java

Both JExcelAPI and Jakarta POI (HSSF) are open source software to read & write data from / to Excel spreadsheet even on non-Microsoft platforms. Sia JExcelAPI e Jakarta POI (HSSF) sono software open source per leggere e scrivere dati da / per foglio di calcolo Excel anche a non Microsoft piattaforme. In my tests HSSF came out to be the clear leader and recommended solution because of robustness and features. Nel mio test HSSF è venuto per essere il leader e chiara soluzione consigliata a causa di robustezza e funzionalità.