Created
July 10, 2020 12:02
-
-
Save datacrat/31f12258764c9ee6e9d2888af39ecb70 to your computer and use it in GitHub Desktop.
JasperReports Integration Example 2 (using Subreport)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.datacrat; | |
import java.util.*; | |
import java.io.*; | |
import net.sf.jasperreports.engine.*; | |
import net.sf.jasperreports.engine.util.*; | |
import net.sf.jasperreports.engine.data.*; | |
/** | |
* Class meisanhin_bunkazai | |
* | |
*/ | |
public class meisanhin_bunkazai | |
{ | |
public static void main( String[] args ) | |
{ | |
// args[0]: 名産品 CSV ファイルパス | |
// args[1]: 文化財 CSV ファイルパス | |
// args[2]: 出力 CSV ファイルパス | |
if (args.length != 3) { | |
System.err.println("Usage: java -jar <This JAR> <Meisanhin CSV> <Bunkazai CSV> <PDF to output>"); | |
System.exit(1); | |
} | |
// リソースフォルダの中の meisanhin-bunkazai.jasper ファイルを読み込んで,jasperReport オブジェクトとする | |
final String mainJasper = "/meisanhin-bunkazai.jasper"; | |
JasperReport jasperReport = null; | |
try { | |
InputStream mainJasperStream = meisanhin_bunkazai.class.getResourceAsStream(mainJasper); | |
jasperReport = (JasperReport) JRLoader.loadObject(mainJasperStream); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
// 引数で得た CSV ファイルをデータソースにする(名産品) | |
File meisanhinFile; | |
JRCsvDataSource meisanhinDataSource = null; | |
try { | |
meisanhinFile = new File(args[0]); | |
if (!meisanhinFile.isFile() || !meisanhinFile.canRead()) { | |
System.err.println("Cannot read " + args[0] + ". Exiting."); | |
System.exit(1); | |
} | |
meisanhinDataSource = new JRCsvDataSource(meisanhinFile, "UTF-8"); | |
meisanhinDataSource.setUseFirstRowAsHeader(true); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
// 引数で得た CSV ファイルをデータソースにする(文化財) | |
File bunkazaiFile; | |
JRCsvDataSource bunkazaiDataSource = null; | |
try { | |
bunkazaiFile = new File(args[1]); | |
if (!bunkazaiFile.isFile() || !bunkazaiFile.canRead()) { | |
System.err.println("Cannot read " + args[1] + ". Exiting."); | |
System.exit(1); | |
} | |
bunkazaiDataSource = new JRCsvDataSource(bunkazaiFile, "UTF-8"); | |
bunkazaiDataSource.setUseFirstRowAsHeader(true); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
// CSV データソースの内容で jasperReport を Fill (はめ込み動作)して | |
// ready-to-print ドキュメントである jasperPrint を作成する. | |
// 今回「Parameter はめ込み」を実施 | |
Map<String, Object> parameters = new HashMap<>(); | |
parameters.put("meisanhinCsvDataSource", meisanhinDataSource); | |
parameters.put("bunkazaiCsvDataSource", bunkazaiDataSource); | |
JasperPrint jasperPrint = null; | |
try { | |
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
// 最後に、jasperPrint を PDF に出力する | |
try { | |
JasperExportManager.exportReportToPdfFile(jasperPrint, args[2]); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
} | |
} | |
/* bottom of file */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment