Apache POI – JavaでExcelファイルを読み書きする
この記事では、https://poi.apache.org/[Apache POI]を使用してExcelファイルを読み書きする方法について説明します。
1. Apache POIライブラリの基本定義
このセクションでは、Excelの読み書き中に使用される基本クラスについて簡単に説明します。
-
`HSSF`は操作を示すためにクラス名の前に置かれます
Microsoft Excel 2003ファイルに関連しています。
-
`XSSF`は操作を示すためにクラス名の前に置かれます
Microsoft Excel 2007以降のファイルに関連しています。
-
XSSFWorkbook`と
HSSFWorkbook`はExcelとして動作するクラスです
ワークブック
。
HSSFSheet`と
XSSFSheet`はExcelとして動作するクラスです
ワークシート
。 `Row`はExcel行を定義します
-
「セル」は、行を参照してアドレス指定されたExcelセルを定義します.
2. Apache POIをダウンロードする
Apache POIライブラリは、Mavenの依存関係を使用して簡単に利用できます。
pom.xml
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.15</version> </dependency>
3. Apache POIライブラリ – シンプルなExcelを書く
次のコードは、Apache POIライブラリを使用して簡単なExcelファイルを作成する方法を示しています。このコードは、2次元データ配列を使用してデータを保持します。
データは `XSSFWorkbook`オブジェクトに書き込まれます。 `XSSFSheet`は作業中のワークシートです。コードは次のとおりです。
ApachePOIExcelWrite.java
package com.techfou; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class ApachePOIExcelWrite { private static final String FILE__NAME = "/tmp/MyFirstExcel.xlsx"; public static void main(String[]args) { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Datatypes in Java"); Object[][]datatypes = { {"Datatype", "Type", "Size(in bytes)"}, {"int", "Primitive", 2}, {"float", "Primitive", 4}, {"double", "Primitive", 8}, {"char", "Primitive", 1}, {"String", "Non-Primitive", "No fixed size"} }; int rowNum = 0; System.out.println("Creating excel"); for (Object[]datatype : datatypes) { Row row = sheet.createRow(rowNum++); int colNum = 0; for (Object field : datatype) { Cell cell = row.createCell(colNum++); if (field instanceof String) { cell.setCellValue((String) field); } else if (field instanceof Integer) { cell.setCellValue((Integer) field); } } } try { FileOutputStream outputStream = new FileOutputStream(FILE__NAME); workbook.write(outputStream); workbook.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("Done"); } }
上記のコードを実行すると、以下のように出力されます。
4. Apache POIライブラリ – Excelファイルの読み込み
以下のコードは、Apache POIライブラリを使用してExcelファイルを読み込む方法を説明しています。関数
getCellTypeEnum`はバージョン3.15では廃止され、バージョン4.0以降は
getCellType`に名称変更されます。
ApachePOIExcelRead.java
package com.techfou; import org.apache.poi.ss.usermodel.** ; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Iterator; public class ApachePOIExcelRead { private static final String FILE__NAME = "/tmp/MyFirstExcel.xlsx"; public static void main(String[]args) { try { FileInputStream excelFile = new FileInputStream(new File(FILE__NAME)); Workbook workbook = new XSSFWorkbook(excelFile); Sheet datatypeSheet = workbook.getSheetAt(0); Iterator<Row> iterator = datatypeSheet.iterator(); while (iterator.hasNext()) { Row currentRow = iterator.next(); Iterator<Cell> cellIterator = currentRow.iterator(); while (cellIterator.hasNext()) { Cell currentCell = cellIterator.next(); //getCellTypeEnum shown as deprecated for version 3.15 //getCellTypeEnum ill be renamed to getCellType starting from version 4.0 if (currentCell.getCellTypeEnum() == CellType.STRING) { System.out.print(currentCell.getStringCellValue() + "--"); } else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) { System.out.print(currentCell.getNumericCellValue() + "--"); } } System.out.println(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
上記のコードを実行すると、以下の出力が得られます。
Datatype--Type--Size(in bytes)-- int--Primitive--2.0-- float--Primitive--4.0-- double--Primitive--8.0-- char--Primitive--1.0-- String--Non-Primitive--No fixed size--
参考文献
getCellTypeEnumの非推奨について]。
https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html#getCellType(
)[Apache
償却に関するPOI参照]。
https://mvnrepository.com/artifact/org.apache.poi/poi
[Apache POI Maven
レポ]。
Apache POI API docs