如何在DynamicJasper中将jrxml文件用作设计模板 [英] How to use jrxml file as design template in DynamicJasper

查看:103
本文介绍了如何在DynamicJasper中将jrxml文件用作设计模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 DynamicJasper 打印报告.到目前为止,它是作为表格打印的.但我想将其打印为支票.我已经将检查模板设计创建为 jrxml .我想将表中填充的值传递给相关的 jrxml 位置.

I am using DynamicJasper to print a report. So far it is printing as a table. But I want to print it as a cheque. I have created the cheque template design as jrxml. I want to pass values which are populated in the table to be passed in to the relevant jrxml position.

以下是打印表格的代码:

Here is the code which prints the table:

public class ReportPrint {

public static void print(Cheque cheque) throws JRException, IOException {
    try {
        DynamicReportBuilder dynamicReportBuilder = new DynamicReportBuilder();

        // configure report-level settings
        dynamicReportBuilder.setReportName("Cheque");

        dynamicReportBuilder.setPageSizeAndOrientation(Page.Page_Letter_Portrait());

        // add cheque number column to report
        ColumnBuilder columnBuilderID = ColumnBuilder.getNew();

        columnBuilderID.setTitle("Cheque Number");
        columnBuilderID.setWidth(100);
        columnBuilderID.setFixedWidth(true);
        columnBuilderID.setColumnProperty("chqNum", String.class.getName(), "chqNum");

        dynamicReportBuilder.addColumn(columnBuilderID.build());

     // add payee name column to report
        ColumnBuilder columnBuilderName = ColumnBuilder.getNew();

        columnBuilderName.setTitle("Payee Name");
        columnBuilderName.setWidth(120);
        columnBuilderName.setFixedWidth(true);
        columnBuilderName.setColumnProperty("name", String.class.getName(), "name");

        dynamicReportBuilder.addColumn(columnBuilderName.build());

        // add amount column to report
        ColumnBuilder columnBuilderAmount = ColumnBuilder.getNew();

        columnBuilderAmount.setTitle("Cheque Amount");
        columnBuilderAmount.setWidth(100);
        columnBuilderAmount.setFixedWidth(true);
        columnBuilderAmount.setColumnProperty("amount", Double.class.getName(), "amount");

        dynamicReportBuilder.addColumn(columnBuilderAmount.build());

        // add date column to report
        ColumnBuilder columnBuilderDate = ColumnBuilder.getNew();

        columnBuilderDate.setTitle("Cheque Date");
        columnBuilderDate.setWidth(100);
        columnBuilderDate.setFixedWidth(true);
        columnBuilderDate.setColumnProperty("date", Date.class.getName(), "date");

        dynamicReportBuilder.addColumn(columnBuilderDate.build());

        // add value in words column to report
        ColumnBuilder columnBuilderWordVal = ColumnBuilder.getNew();

        columnBuilderWordVal.setTitle("Cheque Amount in Words");
        columnBuilderWordVal.setWidth(150);
        columnBuilderWordVal.setFixedWidth(true);
        columnBuilderWordVal.setColumnProperty("value", String.class.getName(), "value");

        dynamicReportBuilder.addColumn(columnBuilderWordVal.build());

        DynamicReport dynamicReport = dynamicReportBuilder.build();

        //Creating data source 
        Collection<Cheque> reportCollection = new ArrayList<Cheque>();
        reportCollection.add(cheque);
        JRDataSource dataSource = new JRBeanCollectionDataSource( reportCollection  );


        // build JasperPrint instance, filling the report with data from data source created above
        JasperPrint jasperPrint = DynamicJasperHelper.generateJasperPrint(
            dynamicReport, new ClassicLayoutManager(), dataSource, new HashMap<String, Object>());

        // export to pdf                     
        String pdfFile = "Cheque.pdf";

        JRExporter exporter = new JRPdfExporter();

        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, pdfFile);

        exporter.exportReport();

        JasperPrintManager.printReport(jasperPrint, true);
    } catch(JRException e) {
        e.printStackTrace();
    }
}
 }

这是 jrxml 文件:

<?xml version="1.0" encoding="UTF-8"?>

<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
    <![CDATA[]]>
</queryString>
<field name="amount" class="java.lang.Double">
    <fieldDescription><![CDATA[amount]]></fieldDescription>
</field>
<field name="chqNum" class="java.lang.String">
    <fieldDescription><![CDATA[chqNum]]></fieldDescription>
</field>
<field name="date" class="java.sql.Date">
    <fieldDescription><![CDATA[date]]></fieldDescription>
</field>
<field name="name" class="java.lang.String">
    <fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<background>
    <band splitType="Stretch"/>
</background>
<detail>
    <band height="128" splitType="Stretch">
        <textField>
            <reportElement uuid="8cd5eac7-d03e-4085-b39f-b1f8d6596b97" x="339" y="101" width="138" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{amount}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement uuid="6c962fb1-7db6-47d5-9a3e-098f4603be1b" x="36" y="59" width="138" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement uuid="961a90da-700b-4e18-a9c6-8bc510dc92af" x="339" y="23" width="138" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
        </textField>
        <staticText>
            <reportElement uuid="48026b8c-f4d0-464e-a3cc-3762316dca2a" stretchType="RelativeToBandHeight" x="19" y="93" width="288" height="35"/>
            <textElement>
                <paragraph lineSpacing="1_1_2" firstLineIndent="36"/>
            </textElement>
            <text><![CDATA[Four Thousand Seven Hundred and Twenty-Five Cents]]></text>
        </staticText>
    </band>
</detail>
</jasperReport>

推荐答案

万一有人在2018/2019年需要它,这是食谱.

In case anyone needs this in 2018/2019 here's a recipe.

1º您拿起jrxml并根据:进行清理: http://dynamicjasper.com/2010/10/06/how-to-to-custom-jrxml-templates/

基本上需要:

  • 模板必须没有组,如果满足以下条件,DynamicJAsper将创建它们:

  • The template must not have groups, DynamicJAsper will create them if necessary.

详细信息栏必须为空:DynamicJasper将在详细信息中使用 带,则任何先前存在的元素都将被删除.

The detail band must be empty: DynamicJasper will work in the detail band, any previously existing element will be deleted.

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

One template per page size and orientation will be needed: This is
because DJ knows how to arrange the element he creates, but not the
existing ones.

2º您调用setTemplateFile方法:

 public DynamicReportBuilder setTemplateFile(String path,
                                                         boolean importFields,
                                                         boolean importVariables,
                                                         boolean importParameters,
                                                         boolean importDatasets)

带有必需的布尔标志.这些布尔标志将允许您加载或不加载在jrxml上设置的参数,字段,变量和数据集.

With the required boolean flags. Those boolean flags will allow you to load or not load the parameters,fields,variables and dataset that you set on your jrxml.

在下面,我附上一个模拟示例.但是,您将需要自己的jrxml文件进行测试.

And below I attach a mock example. However, you are going to need your own jrxml file to test.

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;
        }

    }

}

这篇关于如何在DynamicJasper中将jrxml文件用作设计模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆