列表对象:
@Data
public static class Person{
String id;
String name;
String gender;
String age;
String address;
}
模式一:
把列表数据list传入报表的paramters中,报表table的数据源表达式再去封装;
java代码:
private static void printWithParamTest2() throws IOException {
Map paramMap = new HashMap();
List<Person> mapDataList = new ArrayList<>();
// 参数设置
paramMap.put("conNo", "32166");
paramMap.put("ccc", "123");
Person p1 = new Person();
p1.id = "1";
p1.name = "li";
p1.gender = "man";
p1.age = "10";
p1.address = "beijing";
Person p2 = new Person();
p2.id = "2";
p2.name = "zh";
p2.gender = "women";
p2.age = "13";
p2.address = "shanghai";
Person p3= new Person();
p3.id = "3";
p3.name = "hua";
p3.gender = "man";
p3.age = "16";
p3.address = "guangdong";
mapDataList.add(p1);
mapDataList.add(p2);
mapDataList.add(p3);
paramMap.put("list", mapDataList);
String templatePath = "D:\\doc\\report\\test2.jasper";
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new FileInputStream(templatePath);
JasperReport template = (JasperReport) JRLoader.loadObject(inputStream);
byte[] bytes = JasperRunManager.runReportToPdf(template, paramMap, new JREmptyDataSource());
outputStream = new FileOutputStream("D:\\doc\\report\\test2.pdf");
outputStream.write(bytes);
} catch (Exception e){
e.printStackTrace();
return;
} finally {
if(inputStream != null){
inputStream.close();
}
if(outputStream != null){
outputStream.close();
}
}
}
ireport设计:
在table的数据源的fields中新建参数并“拖入”进table组件中,table的数据源进行如下图配置,编译运行。
结果:
模式二:
模式一是把list作为参数传入,java代码是new了一个空数据源。模式二现在new一个JRBeanCollectionDataSource数据源传入。
在java代码中JRBeanCollectionDataSource需要封装两层,代码如下;
java代码:
private static void printWithParamTest3() throws IOException {
Map paramMap = new HashMap();
List<Person> mapDataList = new ArrayList<>();
// 参数设置
paramMap.put("conNo", "32166");
paramMap.put("ccc", "123");
Person p1 = new Person();
p1.id = "1";
p1.name = "li";
p1.gender = "man";
p1.age = "10";
p1.address = "beijing";
Person p2 = new Person();
p2.id = "2";
p2.name = "zh";
p2.gender = "women";
p2.age = "13";
p2.address = "shanghai";
Person p3= new Person();
p3.id = "3";
p3.name = "hua";
p3.gender = "man";
p3.age = "16";
p3.address = "guangdong";
mapDataList.add(p1);
mapDataList.add(p2);
mapDataList.add(p3);
String templatePath = "D:\\doc\\report\\test3.jasper";
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new FileInputStream(templatePath);
JasperReport template = (JasperReport) JRLoader.loadObject(inputStream);
byte[] bytes;
// 不能直接将jrDataSource 作为 runReportToPdf 的三个参数,否则会自动过滤第一条数据,暂不清楚原因
JRDataSource jrDataSource = new JRBeanCollectionDataSource(mapDataList);
// 使用table组件 需要封装一层才可以
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
map.put("tableData", jrDataSource);
list.add(map);
JRDataSource paramDataSource = new JRBeanCollectionDataSource(list);
bytes = JasperRunManager.runReportToPdf(template, paramMap, paramDataSource);
outputStream = new FileOutputStream("D:\\doc\\report\\test3.pdf");
outputStream.write(bytes);
} catch (Exception e){
e.printStackTrace();
return;
} finally {
if(inputStream != null){
inputStream.close();
}
if(outputStream != null){
outputStream.close();
}
}
}
ireport报表设计:
在报表的fields中 取出列表数据 tableData,然后在table的数据源中配置,然后编译运行。