java 是否可以使用 Jasper Reports 动态生成报告而无需为每个报告生成 jasper?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7370890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to generate reports dynamically using Jasper Reports without generating a jasper for each report?
提问by Naveed S
I have to generate reports based on various parameters which would be provided dynamically. In certain contexts, the parameters may be null. For example, from the table Personwith id, name, age, sexand maritalStatusas fields, I would have to generate reports on married male persons of age 30. Some other times, it may be required to get married female without considering age. If I use the same jasper for both these cases, the age constraint will be null in second case. Is there any way to manage this condition?
我必须根据动态提供的各种参数生成报告。在某些情况下,参数可能为空。例如,从以id、name、age、sex和maritalStatus作为字段的Person表中,我必须生成关于30 岁已婚男性的报告。有时,可能需要与女性结婚而不考虑年龄。如果我对这两种情况使用相同的碧玉,则在第二种情况下年龄约束将为空。有什么办法可以控制这种情况吗?
Also, is it possible to dynamically specify which all fields should be produced in the report?
另外,是否可以动态指定应在报告中生成哪些所有字段?
回答by Alex K
The sample of using the JasperReport APIfor generating report dynamically:
使用JasperReport API动态生成报表示例:
//JasperDesign
JasperDesign jasperDesign = new JasperDesign();
jasperDesign.setName("The dynamically generated report");
jasperDesign.setPageWidth(595);
jasperDesign.setPageHeight(842);
jasperDesign.setColumnWidth(515);
jasperDesign.setColumnSpacing(0);
jasperDesign.setLeftMargin(40);
jasperDesign.setRightMargin(40);
jasperDesign.setTopMargin(50);
jasperDesign.setBottomMargin(50);
//Query
JRDesignQuery query = new JRDesignQuery();
query.setText("SELECT * FROM Address $P!{OrderByClause}");
jasperDesign.setQuery(query);
//Fields
JRDesignField field = new JRDesignField();
field.setName("Id");
field.setValueClass(java.lang.Integer.class);
jasperDesign.addField(field);
field = new JRDesignField();
field.setName("FirstName");
field.setValueClass(java.lang.String.class);
jasperDesign.addField(field);
field = new JRDesignField();
field.setName("LastName");
field.setValueClass(java.lang.String.class);
jasperDesign.addField(field);
//some code
//Detail
band = new JRDesignBand();
band.setHeight(40);
JRDesignStaticText staticText = new JRDesignStaticText();
staticText.setX(0);
staticText.setY(0);
staticText.setWidth(60);
staticText.setHeight(20);
staticText.setMode(ModeEnum.OPAQUE);
staticText.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
staticText.setStyle(boldStyle);
staticText.setText("ID: ");
staticText.getLineBox().getLeftPen().setLineWidth(1);
staticText.getLineBox().getTopPen().setLineWidth(1);
staticText.getLineBox().setLeftPadding(10);
band.addElement(staticText);
textField = new JRDesignTextField();
textField.setX(60);
textField.setY(0);
textField.setWidth(200);
textField.setHeight(20);
textField.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
textField.setStyle(normalStyle);
expression = new JRDesignExpression();
expression.setValueClass(java.lang.Integer.class);
expression.setText("$F{Id}");
textField.setExpression(expression);
textField.getLineBox().getTopPen().setLineWidth(1);
textField.getLineBox().getRightPen().setLineWidth(1);
textField.getLineBox().setLeftPadding(10);
band.addElement(textField);
staticText = new JRDesignStaticText();
staticText.setX(0);
staticText.setY(20);
staticText.setWidth(60);
staticText.setHeight(20);
staticText.setMode(ModeEnum.OPAQUE);
staticText.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
staticText.setStyle(boldStyle);
staticText.setText("Name: ");
staticText.getLineBox().getLeftPen().setLineWidth(1);
staticText.getLineBox().getBottomPen().setLineWidth(1);
staticText.getLineBox().setLeftPadding(10);
band.addElement(staticText);
textField = new JRDesignTextField();
textField.setStretchWithOverflow(true);
textField.setX(60);
textField.setY(20);
textField.setWidth(200);
textField.setHeight(20);
textField.setPositionType(PositionTypeEnum.FLOAT);
textField.setStyle(normalStyle);
expression = new JRDesignExpression();
expression.setValueClass(java.lang.String.class);
expression.setText("$F{FirstName} + \" \" + $F{LastName}");
textField.setExpression(expression);
textField.getLineBox().getRightPen().setLineWidth(1);
textField.getLineBox().getBottomPen().setLineWidth(1);
textField.getLineBox().setLeftPadding(10);
band.addElement(textField);
((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);
You can find more samples in %JasperReportsFolder%/demo/samplesfolder from the JasperReports distribution package.
您可以在JasperReports 分发包的%JasperReportsFolder%/demo/samples文件夹中找到更多示例。
回答by Aasiz
You should try Dynamic jasper. This is exactly the type of use case the dynamic jasper is designed for. You can use same template to generate reports with different columns.
你应该试试动态碧玉。这正是动态碧玉设计的用例类型。您可以使用相同的模板生成具有不同列的报告。
For more info : http://dynamicjasper.com/
欲了解更多信息:http: //dynamicjasper.com/