Overview概要

There are two good choices for reading & writing Microsoft Excel Spreadsheet files from Java, in a platform independent way, - jexcelapi and Jakarta POI (HSSF) .良い選択肢には2つのリーディング&ライティングMicrosoft ExcelスプレッドシートファイルをJavaでは、プラットフォームに依存しないように、 -j excelapiとャカルタP OI( H SSF)です。 Both of them provide nice interface to access Excel data structure and even generate new spreadsheet.ニースの両方のインターフェイスへのアクセスを提供してExcelのデータ構造、さらには新しいスプレッドシートを生成します。 I have done extensive tests with both of them for a high-profile project for a Fortune 500 company.私は大規模なテストを完了して、高の両方のプロジェクトは、フォーチュン500企業のプロファイルです。 Previously also I had successfully used HSSF for another high profile client.私は、以前は次のまた別のハイプロファイルを正常に使用されるクライアントhssfです。 In the paragraphs below I present my conclusions and sample code for reading Excel spreadsheet from Java using both the libraries.下記の段落は、現在の私の結論と私のサンプルコードを読み取り、 JavaからExcelのスプレッドシートの両方のライブラリを使用しています。

Comparison of JExcelAPI with Jakarta-POI (HSSF) jexcelapiとの比較ジャカルタ- POI ( HSSF )ある

1. 1 。 JExcelAPI jexcelapi is clearly not suitable for important data.重要なデータに適していないことは明らかです。 It fails to read several files.いくつかのファイルを読むことに失敗しています。 Even when it reads it fails on cells for unknown reasons.たとえそれが失敗する細胞を読み込むと、未知の理由が考えられます。 In short JExcelAPI isn’t suitable for enterprise use.端的にjexcelapiではない企業での使用に適しています。

2. 2 。 HSSF hssf is the POI Project’s pure Java implementation of the Excel ‘97(-2002) file format.ポイは、プロジェクトの純粋なJavaの実装、 Excelの'97 ( -2002 )ファイル形式です。 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.これは成熟した製品と難なく読むことができたとExcelデータを正常に様々なソースから生成さなど、非MS Excelの製品のようなオープンオフィス、および様々なバージョンのExcelのです。 It is very robust and well featured.ことは非常に堅牢でよく目玉商品です。 Highly recommended.強くお勧めします。

3. 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.パフォーマンスを考慮弊社のテストではなかったため、 1 )データの整合性は、 1つの最も重要な要因とB )が表示されるしなかった任意のテストの実行中に重大なパフォーマンスの違い;両方かれらの多くは非常に高速です。 We didn’t bother to time it for the above reasons.私たちをわざわざ時間をしなかったため、上記の理由が考えられます。

How to read Excel Excel Spreadsheet from Java using Jakarta POI (HSSF) ExcelのExcelのスプレッドシートを読み取る方法を使用して、 Javaからジャカルタ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(); } 試し( poifsfilesystem fs =新しいpoifsfilesystem (新fileinputstream ( ファイル ) ) ; hssfworkbookのwb =新しいhssfworkbookの( FS ) ; hssfsheetシート= wb.getsheetat ( 0 ) ; hssfrow行; hssfcell細胞; int行; / /いいえの行を行= sheet.getphysicalnumberofrows ( ) ; int列= 0 ; / /いいえの列のint tmp = 0 ; / /このトリックにより、私たちも入手してデータが正しく表示されない場合はスタートから最初のいくつかの行を挿入する( int私= 0 ;私< 10 | |私<行;私+ + ) (行= sheet.getrow (私) ;場合(行! = nullが) ( tmp = sheet.getrow (私) 。 getphysicalnumberofcells ( ) ;場合( tmp >列)列= tmp ; ) )を( int研究= 0 ;研究<行;研究+ + ) (行= sheet.getrow (研究) ;場合(行! = nullが) (を( int c = 0 ; c <列; C + +の) (細胞= row.getcell ( (ショート) c ) ;場合(セル! = nullが) ( / /あなたのコードをここに) ) ) ) )キャッチ(例外ioe ) ( ioe.printstacktrace ( ) ; ) 

This sample should get you started.このサンプルでは、取得を開始します。 Don’t forget to import appropriately.適切にインポートすることを忘れないでください。

Gotchas while using Jakarta POI (HSSF)雑多な事柄を使用してジャカルタPOI ( HSSF )

  • getPhysicalNumberOfRows() returns the physical number of rows which may be more than the actual (logical) number of rows. getphysicalnumberofrows ( )戻り値の物理的なレコードの数を超えることがありますが、実際の(論理)の行数をします。 The same goes for getPhysicalNumberOfCells().同じことが言えるのgetphysicalnumberofcells ( ) 。
  • You should check for nulls when fetching the HSSFRow and HSSFCell objects as shown.ヌルときにチェックする必要がhssfrowとhssfcellオブジェクトを取得して表示されます。
  • Remember that Excel tables are often sparsely populated. Excelのテーブルが頻繁に注意して過疎です。 So choose your data structures accordingly.したがってためのデータ構造を選択してください。
  • POI accesses the data by sheet.ポイにアクセスするデータをシートです。 In JExcelAPI you can directly access the data in any row and column.に直接アクセスすることができます。 jexcelapiの任意の行と列のデータです。

How to access Excel Spreadsheet using JExcelAPI 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 =新しいファイル(ファイル) ;試し(ワークブックのwb = workbook.getworkbook ( fp ) ;シートシート= wb.getsheet ( 0 ) ; intカラム= sheet.getcolumns ( ) ; int行= sheet.getrows ( ) ;文字列データ;を( int列= 0 ;列<列;列+ + ) (を( int行= 0 ;行<行;行+ + ) (データ= sheet.getcell (列、行) 。 getcontents ( ) ; / /してコードをここに) ) )キャッチ(例外ioe ) ( system.out.println ( "エラー: " + ioe ) ; ) 

Gotchas while using JExcelAPI雑多な事柄を使用してjexcelapi

  • JExcelAPI may often fail to fetch the data from certain cells or even the whole sheet. jexcelapi年5月に失敗する多くの場合、特定の細胞からデータを取得するかさえ、全体のシートです。 Unfortunately it gives a warning instead of an error to indicate the problem.残念ながら、これに代わりにエラーが発生したことを示す警告してください。
  • JExcelAPI doesn’t expose the full meta-data of the spreadsheet like POI does.公開のフルjexcelapiはありませんのデータを、スプレッドシートのようなメタポイことができます。
  • JExcelAPI doesn’t properly recognize the data type in cells. jexcelapiはありません細胞のデータ型を正しく認識しています。 In all cases it indicated String data in our tests even when there were numeric or date fields.すべてのケースが示す文字列データがあったときに弊社のテストでも数値または日付の欄に入力します。

Concluding thoughts on accessing Excel spreadsheets from Java Excelスプレッドシートにアクセスする考えを締結よりジャワ

Both JExcelAPI and Jakarta POI (HSSF) are open source software to read & write data from / to Excel spreadsheet even on non-Microsoft platforms.両方のjexcelapiとジャカルタPOI ( HSSF )は、オープンソースのソフトウェアからデータを読む&書く/ Excelのスプレッドシートをマイクロソフト以外のプラットフォームでもあります。 In my tests HSSF came out to be the clear leader and recommended solution because of robustness and features.私のテストhssfが出て推奨される解決策のために明確な指導者と堅牢さ、および特徴があります。