JasperReport - 在构建了大量 JRXML 报告后使用 DynamicJasper

JasperReport - Using DynamicJasper after having a lot of JRXML reports built

JasperReport - 在构建大量 JRXML 报告后使用 DynamicJasper

问题概述

目前我的 java 软件使用编译的 .jrxml 文件生成报告。但是,用户抱怨他们想要自定义报告。这基本上是所以我发现了 DynamicJasper。问题是我之前构建的即用型 .jasper 报告有自己的布局,我最终调整了布局,为通过参数发送的徽标插入图像占位符。


问题

有没有办法以简单的方式复制样式或重新使用这些以前构建的报表布局?


据我所知,我使用名为 Cherry 的 Jasper 模板构建了这些报告。有些我用风景,有些我用肖像。


由于问题本身的限制,我不认为这个问题是重复的:我不能将每个报告重建为 DynamicJasper,也不能积极地更改布局。

参考资料

How to use jrxml file as design template in DynamicJasper

所以为了回答我自己的问题,这里有一个食谱。

1º 你把你的 jrxml 并根据 : 清理它 http://dynamicjasper.com/2010/10/06/how-to-use-custom-jrxml-templates/

基本上需要:

  • 模板不能有组,DynamicJAsper 会创建它们 必要的。

  • 详细信息带必须为空:DynamicJasper 将在详细信息中工作 band,任何以前存在的元素都将被删除。

  • 每个页面大小和方向需要一个模板:这是
    因为 DJ 知道如何排列他创建的元素,但不知道
    现有的。

2º 你调用setTemplateFile方法:

 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 Whosebug readers 1";

        Fruit f2 = new Fruit();
        f2.name = "Apple Ag";
        f2.description = "Yummy yummy apple for the Whosebug readers 2";

        Fruit f3 = new Fruit();
        f3.name = "Apple Mn";
        f3.description = "Yummy yummy apple for the Whosebug readers 3";

        Fruit f4 = new Fruit();
        f4.name = "Apple O2";
        f4.description = "Yummy yummy apple for the Whosebug 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;
        }

    }

}