Is it possible to generate reports dynamically using Jasper Reports without generating a jasper for each report?
Asked Answered
F

2

9

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 Person with id, name, age, sex and maritalStatus as 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?

Also, is it possible to dynamically specify which all fields should be produced in the report?

Furore answered 10/9, 2011 at 9:39 Comment(3)
You can easily build query based on parameters. Do you want dynamically set the columns quantity and their order?Lien
@Alex K But when any of the parameters goes null (when some constrains should not be considered, as the age in the second case in my question) no records will be obtained. Also, I want to set the columns quantity and order dynamically.Furore
With the help of IReport it would be difficult to build template with floating colimn's quantity. You can set column's visibility with expression, but the template will be too complex. If you can use javacode it will be more easier.Lien
L
13

The sample of using the JasperReport API for generating report dynamically:

    //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/samples folder from the JasperReports distribution package.

Lien answered 12/9, 2011 at 6:18 Comment(5)
Thanks for your help. I think it would be better for me to use dynamic jasper as there can be a number of possible combinations of required conditions arising in future. Doesn't it provide a more flexible way to manage the columns' visibility dynamically?Furore
@NaveedS Just add sample of building report with APILien
I have done the report generation using dynamic jasper as follows : FastReportBuilder fastReportBuilder = new FastReportBuilder(); for (Field field : requiredFields()) { fastReportBuilder.addColumn(field.getTitle(), field.getName(), field.getDataType(), 50);} fastReportBuilder.setQuery("someQuery", "sql"); fastReportBuilder.setTitle("someTitle"); DynamicReport dynamicReport = fastReportBuilder.build(); JasperPrint jasperPrint = DynamicJasperHelper.generateJasperPrint(dynamicReport, new ClassicLayoutManager(), sqlConnection, null);Furore
I haven't done anything for formatting or styling the appearance (to be done later). I just cared about generating the contents dynamically.Furore
Only to say this source code can be found in the file NoXmlDesignApp.java. Thanks for your answerAbaca
S
1

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/

Staley answered 17/4, 2015 at 16:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.