ireport5.6.0安装不多说
安装完成后启动可能闪退,主要是ireport5.6.0需要jdk1.7才能运行,1.8就会闪退。
修改 C:/Program Files (x86)/Jaspersoft/iReport-5.6.0/etc/ireport.conf (默认路径) 下
#jdkhome="/path/to/jdk" jdkhome="C:/programs/Java/jdk1.7.0_71"
① 对应代码中paramMap中的变量
② 对应代码中datas中的数据的名称
③ 计算数,需要注意计算类型
public static void main(String[] args) {
String exportType = "xlsx";
String defaultTemplatePath = "D://report1.jasper";
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("usid", "test@test.cn");
paramMap.put("usna", "张三");
paramMap.put("curDate", DateUtil.date2String(new Date()));
List<Map<String, ?>> datas = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Map<String, Object> temp = new HashMap<>();
temp.put("key1", "key----" + i);
temp.put("val1", "val====" + i);
temp.put("cnt", 1);
datas.add(temp);
}
JRDataSource dataSource = new JRMapCollectionDataSource(datas);
try {
JasperPrint jasperPrint = JasperFillManager.fillReport(defaultTemplatePath, paramMap, dataSource);
String targetFileName = "D://test_" + DateUtil.date2String(new Date(), DateUtil.yyyyMMddHHmmss) + "." + exportType;
if ("pdf".equalsIgnoreCase(exportType)) {
JasperExportManager.exportReportToPdfFile(jasperPrint, targetFileName);
} else if ("xlsx".equalsIgnoreCase(exportType)) {
JRXlsxExporter exporter = new JRXlsxExporter();
SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration();
configuration.setWhitePageBackground(true);
configuration.setRemoveEmptySpaceBetweenRows(true);// 空行
configuration.setRemoveEmptySpaceBetweenColumns(true);// 空列
exporter.setConfiguration(configuration);
// 设置输入项
ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
exporter.setExporterInput(exporterInput);
// 设置输出项
OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(targetFileName);
exporter.setExporterOutput(exporterOutput);
exporter.exportReport();
}
System.out.println("导出成功:" + targetFileName);
} catch (Exception e) {
e.printStackTrace();
}
}