【Java】Eclipse環境でjavaからExcelを読み書きする方法ApachePOIを用いて実現

Excel

ad2

【Java】Eclipse環境でjavaからExcelを読み書きする方法ApachePOIを用いて実現

手順

  1. ApachePOIのダウンロード
  2. ApachePOIライブラリを展開
  3. Ecllipseのjavaプロジェクトでビルド・パス構成でjarファイルを追加
  4. ソースコード実装・・・完了

という流れになります。

ApachePOIのダウンロード

まずは、ApacheのサイトからPOIをダウンロードします。

「Binary Distribution」のpoi-bin-4.1.2-20200217.zipをクリックしてダウンロードします。

apache1

ダウンロードして保存する先は任意です。

poizip

ApachePOI.zipの解凍展開

zipファイルをこんどは解凍し展開します。

展開先は任意です。

poifolder

EclipseのJavaプロジェクトでビルド・パス構成設定

EclipseのJavaプロジェクトでビルド・パスの構成を開く

eclipse_javapoi

ライブラリータブを開き、右側にある外部JARの追加ボタンをクリックします。

build_path1

先ほど解凍し展開したフォルダ階層内のpoi-4.1.2 >

poijar1

赤枠のjarファイル追加。

さらに、libフォルダ内の

lib_poi

 

赤枠内の「commons-***」Jarファイルも追加

さらに、ooxml-libフォルダ内の

ooxml-lib_jar

 

赤枠内の「curvesapi-1.06.jar」「xmlbeans-3.1.0.jar」jarファイルを追加します。

jarファイルが追加できたら、

poi_path_build_done

 

適用して完了です。

追加したJarファイル

/poi-4.1.2.jar
/poi-examples-4.1.2.jar
/poi-excelant-4.1.2.jar
/poi-ooxml-4.1.2.jar
/poi-ooxml-schemas-4.1.2.jar
/poi-scratchpad-4.1.2.jar
/lib/commons-codec-1.13.jar
/lib/commons-collections4-4.4.jar
/lib/commons-compress-1.19.jar
/lib/commons-logging-1.2.jar
/lib/commons-math3-3.6.1.jar
/ooxml-lib/curvesapi-1.06.jar
/ooxml-lib/xmlbeans-3.1.0.jar

どれが必要か不要なのか詳しくないので、適当に追加してみました。

Excelファイルを出力

jarファイルも追加できたら、あとはExcelを操作する方法ですね。

簡単な書き込み処理を書いたソースコード

package poi_excel;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class main {
	public static void main(String[] args) {
		XSSFWorkbook workbook = new XSSFWorkbook();
		Sheet sheet = workbook.createSheet("test");
		Row row0 = sheet.createRow(0);
		Row row1 = sheet.createRow(1);
		Row row2 = sheet.createRow(2);

		Cell cell0 = row0.createCell(0);
		Cell cell1 = row1.createCell(1);
		Cell cell2 = row2.createCell(2);

		cell0.setCellValue("Excelを出力してみた");
		cell1.setCellValue(100 / 3.14);
		cell2.setCellValue(12345.9876);

	    FileOutputStream output = null;
	    try{
	    output = new FileOutputStream("sampleExcelout.xlsx");
	      workbook.write(output);
	      System.out.println("完了。。");
	    }catch(IOException e){
	      System.out.println(e.toString());
	    }finally{
	      try {
	    	  if (output != null) {
	    		  	output.close();
	    	      }
	        if (workbook != null) {
	            	workbook.close();
	          }
	      }catch(IOException e){
	        System.out.println(e.toString());
	      }
	    }
	}
}

 

実行結果

excel_output

文字列、計算式の結果、数値が書き込まれて出力できました!!!

Excel読み込み

Excelを用意する。

excel_read

セル”C3″、”E2″、”F5″、”A1″の値を取得するコードを書いてみる。

package poi_excel;

import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class main {
	public static void main(String[] args) {

		String excelread = "sampleExcelread.xlsx";
		XSSFWorkbook workbook  = null;
		try{
			workbook = new XSSFWorkbook(excelread);
			//左端のシートを取得する
			Sheet sheet = workbook.getSheetAt(0);

			//取得する値のセル設定
			Row row0 = sheet.getRow(2);  //3行目
			Row row1 = sheet.getRow(1);  //2行目
			Row row2 = sheet.getRow(4);  //5行目
			Row row3 = sheet.getRow(0);  //1行目

			Cell cell0 = row0.getCell(2);   //C列
			Cell cell1 = row1.getCell(4);   //E列
			Cell cell2 = row2.getCell(5);   //F列
			Cell cell3 = row3.getCell(0);   //A列

			//取得した値をコンソールに出力する
			System.out.println(cell0.getNumericCellValue());
			System.out.println(cell1.getStringCellValue());
			System.out.println(cell2.getNumericCellValue());
			System.out.println(cell3.getStringCellValue());

		    System.out.println("完了。。");
	    }catch(IOException e){
	      System.out.println(e.toString());
	    }finally{
	      try {
	        if (workbook != null) {
	            	workbook.close();
	          }
	      }catch(IOException e){
	        System.out.println(e.toString());
	      }
	    }
	}
}

 

実行結果

excel_read_result

指定したセルの値が取得できました!!!

まとめ

JavaでExcelを操作するにはApachePOIを用いれば容易にできることがわかりました。

これを応用して業務の効率化に役立たせましょう!!

では。(@^^)/~~~