Excellaのインストール
build.gradleにexcellaを指定する
dependencies {
compile("org.bbreak.excella:excella-core:1.12")
compile("org.bbreak.excella:excella-reports:1.11")
compile("org.bbreak.excella:excella-trans:1.8")
}
Excelを出力する
Excelテンプレートを作成
以下を参考にExcelのテンプレートを作成する
http://excella-core.github.io/excella-core/reference/index.html
http://excella-core.osdn.jp/excella-reports/reference/index.html
Javaコード
ExcellMakeService.java
ReportBook outputBook = new ReportBook("C:\\templace\\template.xlsx", "./out", ExcelExporter.FORMAT_TYPE);
ReportSheet outputSheet = new ReportSheet("report");
outputBook.addReportSheet(outputSheet);
//ここにはテンプレートで定義した変数に値を設定する処理を記述する(省略)
ReportProcessor reportProcessor = new ReportProcessor();
try {
reportProcessor.process(outputBook);
} catch (Exception e) {
e.printStackTrace();
}
PDFを出力する
excella-pdfexporterの準備
バージョン1.12ではexcella-pdfexporterが含まれていないため、以下のサイトよりソースを取得する。
https://github.com/excella-core/excella-pdfexporter
上記から取得したソースをプロジェクトに組み入れるかしてビルドする。
Libre Officeの入手
PDFへの変換にはLibre Officeを使用するため以下からダウンロードしインストールする
https://ja.libreoffice.org/
Javaコード
上記のExcellMakeService.javaにコードを追加する
ExcellMakeService.java
System.setProperty("java.io.tmpdir", "/tmp/");
//入手したLibre Officeのパスを設定する。
System.setProperty("office.home", "C:\\Program Files\\LibreOffice");
//ExcelExporter.FORMAT_TYPEをOoPdfExporter.FORMAT_TYPEに変更
ReportBook outputBook = new ReportBook("C:\\templace\\template.xlsx", "./out", OoPdfExporter.FORMAT_TYPE);
ReportSheet outputSheet = new ReportSheet("report");
outputBook.addReportSheet(outputSheet);
//ここにはテンプレートで定義した変数に値を設定する処理を記述する(省略)
ReportProcessor reportProcessor = new ReportProcessor();
//ExporterにOoPdfExporter追加
reportProcessor.addReportBookExporter(new OoPdfExporter());
try {
reportProcessor.process(outputBook);
} catch (Exception e) {
e.printStackTrace();
}