LoginSignup
0
1

More than 5 years have passed since last update.

xlsmapperをちょっとだけ触ってみた

Posted at

これは何

xlsmapperを知ったのでちょっと触ってみたメモ書き

試した環境

  • Java8(jdk1.8.0_102)
  • XlsMapper(1.4.4)
  • eclipse

環境構築

ここに書いてある通りやれば出来る。

試した内容

現時点で欲しかったのはExcelファイルを読み込む方だったので、ファイル読み込みのみ。

Javaコード

PojoSheet.java

import com.gh.mygreen.xlsmapper.annotation.LabelledCellType;
import com.gh.mygreen.xlsmapper.annotation.XlsLabelledCell;
import com.gh.mygreen.xlsmapper.annotation.XlsSheet;

@XlsSheet(name="Sheet1")
public class PojoSheet {

    @XlsLabelledCell(label="midashi1", type=LabelledCellType.Bottom)
    String cell1;
    @XlsLabelledCell(label="midashi2", type=LabelledCellType.Bottom)
    String cell2;
    @XlsLabelledCell(label="midashi3", type=LabelledCellType.Bottom)
    String cell3;
    @XlsLabelledCell(label="midashi4", type=LabelledCellType.Bottom)
    String cell4;
    @XlsLabelledCell(label="midashi5", type=LabelledCellType.Bottom)
    String cell5;

}

Test.java(Mainクラス)

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import com.gh.mygreen.xlsmapper.XlsMapper;
import com.gh.mygreen.xlsmapper.XlsMapperException;

public class Test {

    private static final String xlsxFileAddress = "G:\\work\\Test.xlsx";


    public static void main(String[] args) throws FileNotFoundException, XlsMapperException {

        XlsMapper xlsMapper = new XlsMapper();
        Object[] sheets = xlsMapper.loadMultiple(
                new FileInputStream(xlsxFileAddress),                  // 読み込むExcelファイル
                new Class[]{PojoSheet.class}  // アノテーションを付与したクラス。
                );


        for(Object obj : sheets) {

            PojoSheet s = (PojoSheet)obj;
            System.out.println(s.cell1 + ", " + s.cell2 + ", " + s.cell3 + ", " + s.cell4 + ", " + s.cell5);

        }

    }

}

テストしたExcelファイル

excel2.PNG

処理結果

Excelファイルの内容とJavaコードの組み合わせが正しいとき

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Cell1, Cell2, Cell3, null, Cell5

Excelファイルの内容とJavaコードの組み合わせが正しくないとき(見出しの文言が違うとか)

見出しが一致する項目が無いよ、とエラーを吐いてくれる。

Exception in thread "main" com.gh.mygreen.xlsmapper.fieldprocessor.CellNotFoundException: Cell 'midashi5' not found in sheet 'Sheet1'
    at com.gh.mygreen.xlsmapper.Utils.getCell(Utils.java:1064)
    at com.gh.mygreen.xlsmapper.Utils.getCell(Utils.java:1031)
:

まとめ

良さげなところ

  • 表形式のExcelを読み込んで処理したい、という用途であれば、見出しを定義することで読み込みが容易に出来そう。
  • 表のレイアウトが変わって、見出しは変わらないが、位置が変わったというような場合も対応可能に読める。
  • 普通にApache POIを呼び出すよりはコードがかなりシンプルに書ける。

懸念事項

  • ベースとなっているのはApache POIなので、大容量のExcelファイルを読み込むと恐らくOutOfMemoryErrorを吐くのではないか(要検証)
  • 大容量のファイル書き込みも同様に、遅くなる可能性があるのではないか…。
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1