Jasper reports JasperReport—在构建了大量JRXML报告之后使用DynamicJasper
JasperReport-在构建了大量JRXML报告后使用DynamicJasper 问题概述 目前,我的java软件使用编译的.jrxml文件生成报告。然而,用户抱怨他们想要定制报告。基本上就是这样,我发现了DynamicJasper。问题是,我之前构建的ready-to-use.jasper报告有自己的布局,我最终对其进行了调整,为通过参数发送的徽标插入了图像占位符Jasper reports JasperReport—在构建了大量JRXML报告之后使用DynamicJasper,jasper-reports,dynamic-jasper,Jasper Reports,Dynamic Jasper,JasperReport-在构建了大量JRXML报告后使用DynamicJasper 问题概述 目前,我的java软件使用编译的.jrxml文件生成报告。然而,用户抱怨他们想要定制报告。基本上就是这样,我发现了DynamicJasper。问题是,我之前构建的ready-to-use.jasper报告有自己的布局,我最终对其进行了调整,为通过参数发送的徽标插入了图像占位符 问题 有没有办法复制样式或以简单的方式重复使用这些以前构建的报告布局 据我记忆所及,我使用名为Cherry的Jasper模
问题 有没有办法复制样式或以简单的方式重复使用这些以前构建的报告布局
据我记忆所及,我使用名为Cherry的Jasper模板构建了这些报告。有些我用风景画,有些我用肖像画
由于问题本身的限制,我不认为这个问题是重复的:我不能将每个报告重建为动态的,也不能积极地更改布局 参考资料
为了回答我自己的问题,这里有一个食谱 1º取下jrxml并按照以下步骤进行清理: 基本上,要求:
public DynamicReportBuilder setTemplateFile(String path,
boolean importFields,
boolean importVariables,
boolean importParameters,
boolean importDatasets)
- 模板不能有组,DynamicAsper将在以下情况下创建它们: 必要的
- 详图栏必须为空:DynamicJasper将在详图栏中工作 带,以前存在的任何元素都将被删除
- 每个页面大小和方向需要一个模板:这是
因为DJ知道如何安排他创建的元素,但不知道如何安排
现有的
public DynamicReportBuilder setTemplateFile(String path,
boolean importFields,
boolean importVariables,
boolean importParameters,
boolean importDatasets)
使用所需的布尔标志。这些布尔标志将允许您加载或不加载在jrxml上设置的参数、字段、变量和数据集
下面我附上一个模拟例子。但是,您需要自己的jrxml文件进行测试
public class TestReport {
protected static JasperPrint jp;
protected static JasperReport jr;
protected static Map params = new HashMap();
protected static DynamicReport dr;
public static void main(String args[]) throws SQLException, ColumnBuilderException, ClassNotFoundException {
TestReport t = new TestReport();
t.createReport();
}
public void createReport() throws SQLException, ColumnBuilderException, ClassNotFoundException {
ArrayList<Fruit> createMockDataset = createMockDataset();
Style titleStyle = new Style();
titleStyle.setHorizontalAlign(HorizontalAlign.CENTER);
titleStyle.setFont(Font.ARIAL_SMALL_BOLD);
Style dataStyle = new Style();
dataStyle.setHorizontalAlign(HorizontalAlign.CENTER);
dataStyle.setFont(Font.ARIAL_SMALL);
dataStyle.setBlankWhenNull(true);
final List items = SortUtils.sortCollection(createMockDataset, Arrays.asList(new String[]{"name", "description"}));
FastReportBuilder drb = new FastReportBuilder();
drb.setTemplateFile("templatePortrait.jrxml", true, true, true, true);
drb.addColumn("name", "name", String.class.getName(), 30, dataStyle)
.addColumn("description", "description", String.class.getName(), 50, dataStyle)
.setTitle("Report")
.setSubtitle("")
.setPrintBackgroundOnOddRows(true)
.setUseFullPageWidth(true);
DynamicReport dynamicReport = drb.build();
dynamicReport.setTitleStyle(titleStyle);
HashMap parametros = new HashMap();
parametros.put("dataRelatorio", MyTools.getDataPorExtenso());
doReport(dynamicReport, items, parametros);
}
public void doReport(final DynamicReport _report, final Collection _data, HashMap parametros) {
try {
JRDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(_data);
JasperPrint jasperPrint = DynamicJasperHelper.generateJasperPrint(_report, new ClassicLayoutManager(), beanCollectionDataSource, parametros);
JasperViewer.viewReport(jasperPrint);
} catch (JRException ex) {
ex.printStackTrace();
}
}
public ArrayList<Fruit> createMockDataset() {
ArrayList<Fruit> fruits = new ArrayList<>();
Fruit f1 = new Fruit();
f1.name = "Apple X1";
f1.description = "Yummy yummy apple for the stackoverflow readers 1";
Fruit f2 = new Fruit();
f2.name = "Apple Ag";
f2.description = "Yummy yummy apple for the stackoverflow readers 2";
Fruit f3 = new Fruit();
f3.name = "Apple Mn";
f3.description = "Yummy yummy apple for the stackoverflow readers 3";
Fruit f4 = new Fruit();
f4.name = "Apple O2";
f4.description = "Yummy yummy apple for the stackoverflow readers 4";
//Evaluations for f1
for (int i = 0; i < 4; i++) {
Evaluation e = new Evaluation();
e.id = i;
e.score = Math.random() * 10;
f1.evaluations.add(e);
}
//evaluations for f4
for (int i = 0; i < 4; i++) {
Evaluation e = new Evaluation();
e.id = i;
e.score = Math.random() * 10;
f4.evaluations.add(e);
}
fruits.add(f1);
fruits.add(f2);
fruits.add(f3);
fruits.add(f4);
return fruits;
}
public class Fruit {
public String name;
public String description;
public ArrayList<Evaluation> evaluations = new ArrayList<Evaluation>();
public Fruit() {
}
public Fruit(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public ArrayList<Evaluation> getEvaluations() {
return evaluations;
}
public void setEvaluations(ArrayList<Evaluation> evaluations) {
this.evaluations = evaluations;
}
}
public class Evaluation {
public int id;
public double score;
public Evaluation() {
}
public Evaluation(int id, double score) {
this.id = id;
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
}
公共类测试报告{
防静电茉莉花;
防静电JasperReport jr;
受保护的静态映射参数=new HashMap();
受保护的静态动态端口dr;
公共静态void main(字符串args[])抛出SQLException、ColumnBuilderException、ClassNotFoundException{
TestReport t=newtestreport();
t、 createReport();
}
public void createReport()引发SQLException、ColumnBuilderException、ClassNotFoundException{
ArrayList createMockDataset=createMockDataset();
样式标题样式=新样式();
标题样式。设置水平对齐(水平对齐。中心);
titleStyle.setFont(Font.ARIAL\u SMALL\u BOLD);
样式数据样式=新样式();
dataStyle.setHorizontalAlign(HorizontalAlign.CENTER);
setFont(Font.ARIAL_-SMALL);
dataStyle.setblankhennull(true);
最终列表项=SortUtils.sortCollection(createMockDataset,Arrays.asList(新字符串[]{“name”,“description”}));
FastReportBuilder drb=新的FastReportBuilder();
setTemplateFile(“templategrait.jrxml”,真,真,真,真);
drb.addColumn(“名称”,“名称”,String.class.getName(),30,数据类型)
.addColumn(“说明”,“说明”,String.class.getName(),50,数据类型)
.setTitle(“报告”)
.setSubtitle(“”)
.SetPrintBackgroundOnAddress(真)
.setUseFullPageWidth(真);
DynamicReport DynamicReport=drb.build();
dynamicReport.setTitleStyle(titleStyle);
HashMap parametros=新的HashMap();
parametros.put(“dataRelatorio”,MyTools.getDataPorExtenso());
doReport(动态报告、项目、参数);
}
公共无效数据报告(最终动态报告、最终收集数据、HashMap参数){
试一试{
JRDataSource beanCollectionDataSource=新的JRBeanCollectionDataSource(_数据);
JasperPrint JasperPrint=DynamicJasperHelper.generateJasperPrint(_报告,新ClassClayOutManager(),beanCollectionDataSource,参数);
JasperViewer.viewReport(jasperPrint);
}捕获(JREX异常){
例如printStackTrace();
}
}
公共ArrayList createMockDataset(){
ArrayList=新的ArrayList();
果实f1=新果实();
f1.name=“苹果X1”;
f1.description=“用于stackoverflow阅读器的美味苹果1”;
果f2=新果();
f2.name=“苹果公司”;
f2.description=“用于stackoverflow阅读器的美味苹果2”;
水果f3=新水果();
f3.name=“苹果Mn”;
f3.description=“适用于stackoverflow阅读器的美味苹果3”;
水果f4=新水果();
f4.name=“苹果O2”;
f4.description=“用于stackoverflow阅读器的美味苹果4”;
//f1的评估
对于(int i=0;i<4;i++){
评估e=新评估();
e、 id=i;
e、 分数=Math.random()*10;
f1.评估。添加(e);
}
//对f4的评估
对于(int i=0;i<4;i++){
评估e=新评估();
e、 id=i;
e、 分数=Math.random()*10;
f4.评价。添加(e);
}
添加(f1);
添加(f2);
添加(f3);
添加(f4);
还果;
}
公共级水果{
公共字符串名称;
公共字符串描述;
公共ArrayList求值=新建ArrayList();
公众水果(){
}
公共水果(字符串名称、字符串描述){
this.name=名称;
this.description=描述;
}
公共字符串getName(){
返回名称;
}
公共空集合名(字符串)