从JSON对象或JSON字符串创建Jasper报告PDF表单 [英] Create Jasper Report PDF form from JSON Object or JSON String

查看:121
本文介绍了从JSON对象或JSON字符串创建Jasper报告PDF表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JRBeanCollectionDataSource创建Jasper报告(PDF,Excel,Csv)时很好。这意味着.jrxml文件接受pojo集合作为处理报告的输入。

It is fine when create the Jasper report(PDF, Excel, Csv) by using JRBeanCollectionDataSource. It means that the .jrxml file accepts the collection of pojo as a input to process the report.

现在,我一直在尝试使用相同的方法创建jasper报告。 jrxml但来自JSON Object。
我尝试了以下内容,但所有值在pdf报告中都为空

Now, I have been trying to create the jasper report with the same .jrxml but from JSON Object. I tried the following, but all values are null in pdf report

Resource resource = new ClassPathXmlApplicationContext().getResource("classpath:reports/project.jrxml");
JsonDataSource ds = new JsonDataSource(new File("c:\myjson.json"));
jasperDesign = JRXmlLoader.load(resource.getInputStream());
JasperReport jasperReport  = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,  parameters,  ds);
JasperExportManager.exportReportToPdfFile(jasperPrint, destination+fileName+".pdf");

任何人都可以帮助我吗?

Can any one help me?

推荐答案

我一直在努力使用JSON作为Jasper报告的数据源,并且在网上缺乏不错的例子,我想我会在这里发布这个以供将来参考。

I've just been struggling with using JSON as the DataSource to a Jasper Report and with a lack of decent examples on the Net I thought I would post this here for future reference.

这个例子是如何使用iReport Designer和JSON DataSource。

This example is how to use iReport Designer and a JSON DataSource.

首先,输入JSON:

{ 
    "userName": "Evil Raat",
    "details": {
        "email": "not_really@test.com"
    }
}

然后在iReport Designer中创建一个JSON DataSource并将其指向您的文件(将所有其他详细信息保留为默认值)

Then create a JSON DataSource in iReport Designer and point it at your file (leaving all the other details as their defaults)

接下来,您可以使用以下jrxml模板来呈现将JSON高于报告:

Next you can use the following jrxml template to render the above JSON into a report:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="sample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a894078a-929b-4aae-a1d0-46485f0f8835">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <queryString language="json">
        <![CDATA[]]>
    </queryString>
    <field name="userName" class="java.lang.String">
        <fieldDescription><![CDATA[userName]]></fieldDescription>
    </field>
    <field name="userEmail" class="java.lang.String">
        <fieldDescription><![CDATA[details.email]]></fieldDescription>
    </field>
    <title>
        <band height="200" splitType="Stretch">
            <textField>
                <reportElement uuid="3b74775b-4555-43c3-bdf2-1677145c8660" x="0" y="31" width="555" height="20"/>
                <textElement textAlignment="Right">
                    <font fontName="Helvetica" size="12" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{userName}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="aa6cc7c8-2ca1-4f0f-92e2-c466083daba0" x="0" y="54" width="555" height="20"/>
                <textElement textAlignment="Right">
                    <font fontName="Helvetica" size="12" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{userEmail}]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

注意:您必须首先定义字段元素,然后才可以使用。它们应该是使用标准点表示法从JSON输入文件的根目录开始的JSON路径。有关示例,请参阅上面的fieldDescription元素。

NOTE: You must define the field elements first, before any they can be used. They should be the JSON path from the root of the JSON input file using standard dot notation. See the fieldDescription elements above for examples.

定义字段后,您可以在文本字段中使用其计算值或其他:

Once your field is defined you can use its computed value in a text field or whatever:

希望能帮助一些人。

这篇关于从JSON对象或JSON字符串创建Jasper报告PDF表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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