gpt4 book ai didi

java - 是否可以使用 Jasper Reports 动态生成报告而不为每个报告生成 jasper?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:14:32 24 4
gpt4 key购买 nike

我必须根据动态提供的各种参数生成报告。在某些上下文中,参数可能为空。例如,来自表 Personidnameagesexma​​ritalStatus 作为字段,我必须生成有关 30 岁已婚男性 的报告。有时,可能会要求不考虑年龄而嫁给女性。如果我在这两种情况下都使用相同的碧 Jade ,则在第二种情况下年龄限制将为空。有什么办法可以控制这种情况?

此外,是否可以动态指定应在报告中生成哪些所有字段?

最佳答案

使用 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);

您可以在 JasperReports distribution package%JasperReportsFolder%/demo/samples 文件夹中找到更多示例.

关于java - 是否可以使用 Jasper Reports 动态生成报告而不为每个报告生成 jasper?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7370890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com