目录
一、先安装jasper的编写模板工具
二、制作报表模板
1.打开Jaspersoft Studio软件,新建jasper report模板文件:File→new→Jasper Report
2.开始画表
方法1:把传参数据作为List数据取值
方法2:把传参数据作为数据源来取值
三、java传参
1、读取报表模板
2、装载模板数据
3、打印pdf
四、打印效果
五、jrxml、java文件
我这里不涉及数据库数据打印,主要为Java传参List数据打印,在此提供两种画报表的方法及其对应的Java传参方法,,可以先移步打印效果,再看自己需要选择哦~~~~~~~~
主要有两个工具Jaspersoft Studio、iReport,个人推荐Jaspersoft Studio
1.安装JaspersoftStudio
2.安装jasper的编写工具iReport Design
我这里不涉及数据库数据打印,主要为Java传参List数据打印,在此提供两种方法
这里为了区分新建模板TestMapList2.jrxml
方法1:读取jrxml文件
String jrxml ="C:\Users\qyl\JaspersoftWorkspace\MyReports\TestMapList.jrxml";
JasperReport report = JasperCompileManager.compileReport(jrxml);
方法2:读取jasper文件
FileInputStream is = new FileInputStream("C:\\Users\\qyl\\JaspersoftWorkspace\\MyReports\\TestMapList.jasper");
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(is);
针对制作报表方法1:
// 设置模板数据
Map map=new HashMap();
List
针对制作报表方法2:
// 读取jasper模板文件
JasperReport jasperReport = null;
try {
String jrxml ="C:\\Users\\qyl\\JaspersoftWorkspace\\MyReports\\TestMapList2.jrxml";
jasperReport = JasperCompileManager.compileReport(jrxml);
// 此行代码可解决部分打印空页问题
jasperReport.setWhenNoDataType(WhenNoDataTypeEnum.ALL_SECTIONS_NO_DETAIL);
} catch (JRException e) {
e.printStackTrace();
}
// 设置模板数据
Map map=new HashMap();
List> dataList = new ArrayList>();
for (int i = 0; i < 4; i++) {
Map rowMap1 = new HashMap();
rowMap1.put("C1", "第:" + i + "行1列");
rowMap1.put("C2", "第:" + i + "行1列");
rowMap1.put("C3", "第:" + i + "行1列");
rowMap1.put("C4", "第:" + i + "行1列");
rowMap1.put("C5", "第:" + i + "行1列");
dataList.add(rowMap1);
}
//设置paramters
map.put("tableData", new JRMapCollectionDataSource(dataList));
JasperPrint jasperPrint = null;
try {
jasperPrint = JasperFillManager.fillReport(jasperReport, map, new JREmptyDataSource());
} catch (JRException e) {
e.printStackTrace();
}
方法1:
JRPdfExporter exporter = new JRPdfExporter();
try {
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, new FileOutputStream("D:\\test1.pdf"));
exporter.exportReport();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JRException e) {
e.printStackTrace();
}
方法2:
JasperExportManager.exportReportToPdfStream(jasperPrint, new FileOutputStream("D:\\test1.pdf"));
第一种报表打印效果:
第二种报表打印效果:
TestMapList.jrxml:第一种报表文件
TestJ.java:第一种报表传参方法
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.type.WhenNoDataTypeEnum;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestJ {
public static void main(String args[]) throws FileNotFoundException {
// 读取jasper模板文件
JasperReport jasperReport = null;
try {
String jrxml ="C:\\Users\\qyl\\JaspersoftWorkspace\\MyReports\\TestMapList.jrxml";
jasperReport = JasperCompileManager.compileReport(jrxml);
// 此行代码可解决部分打印空页问题
jasperReport.setWhenNoDataType(WhenNoDataTypeEnum.ALL_SECTIONS_NO_DETAIL);
} catch (JRException e) {
e.printStackTrace();
}
Map map = new HashMap();
// 设置模板数据
List> dataList = new ArrayList>();
for (int i = 0; i < 4; i++) {
Map rowMap1 = new HashMap();
rowMap1.put("C1", "第:" + i + "行1列");
rowMap1.put("C2", "第:" + i + "行1列");
rowMap1.put("C3", "第:" + i + "行1列");
rowMap1.put("C4", "第:" + i + "行1列");
rowMap1.put("C5", "第:" + i + "行1列");
dataList.add(rowMap1);
}
//设置paramters
map.put("dataList", dataList);
JasperPrint jasperPrint = null;
try {
jasperPrint = JasperFillManager.fillReport(jasperReport, map, new JRBeanCollectionDataSource(dataList));
// 生成PDF客户端
JRPdfExporter exporter = new JRPdfExporter();
try {
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, new FileOutputStream("D:\\test1.pdf"));
exporter.exportReport();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JRException e) {
e.printStackTrace();
}
} catch (JRException e) {
e.printStackTrace();
}
}
}
TestMapList2.jrxml:第二种报表文件
TestJ2.java:第二种报表Java传参
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.type.WhenNoDataTypeEnum;
import net.sf.jasperreports.engine.util.JRLoader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.*;
public class TestJ2 {
public static void main(String args[]) throws FileNotFoundException {
// 读取jasper模板文件
JasperReport jasperReport = null;
try {
FileInputStream is = new FileInputStream("C:\\Users\\qyl\\JaspersoftWorkspace\\MyReports\\TestMapList2.jasper");
jasperReport = (JasperReport) JRLoader.loadObject(is);
// 此行代码可解决部分打印空页问题
jasperReport.setWhenNoDataType(WhenNoDataTypeEnum.ALL_SECTIONS_NO_DETAIL);
} catch (JRException e) {
e.printStackTrace();
}
// 设置模板数据
Map map = new HashMap();
List> dataList = new ArrayList>();
for (int i = 0; i < 4; i++) {
Map rowMap1 = new HashMap();
rowMap1.put("C1", "第:" + i + "行1列");
rowMap1.put("C2", "第:" + i + "行1列");
rowMap1.put("C3", "第:" + i + "行1列");
rowMap1.put("C4", "第:" + i + "行1列");
rowMap1.put("C5", "第:" + i + "行1列");
dataList.add(rowMap1);
}
//设置paramters
map.put("tableData", new JRMapCollectionDataSource(dataList));
JasperPrint jasperPrint = null;
try {
jasperPrint = JasperFillManager.fillReport(jasperReport, map, new JREmptyDataSource());
// 生成PDF客户端
JasperExportManager.exportReportToPdfStream(jasperPrint, new FileOutputStream("D:\\test2.pdf"));
} catch (JRException e) {
e.printStackTrace();
}
}
}
如果出现pdf打印,不显示中文问题,可以看看下一篇文章:
Jasper报表导出pdf中文不显示——Font "simsun" is not available to the JVM. See the Javadoc for more det,已解决