使用JasperReports从json填写报告 [英] Filling reports from json using JasperReports

查看:645
本文介绍了使用JasperReports从json填写报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JasperReports的初学者(第一次尝试)所以,
我设置了netbeans,我第一次尝试使用数据库中的数据是成功的。但现在我只想使用Netbeans中的Jasper Reports从json数据创建我的报告而且我认为它不像从数据库中获取数据。



顺便说一下我试图编辑我的xml文件并添加:

 < textField> 
< reportElement x =0y =31width =555height =20uuid =9678a129-61e8-4034-ab8e-739ee5723c87/>
< textElement textAlignment =Right>
< font size =12isBold =true/>
< / textElement>
< textFieldExpression><![CDATA [$ F {nom}]]>< / textFieldExpression>
< / textField>

但它在报告中给我null。



<这是我在java方面所做的:

  Map parametersMap = new HashMap(); 
parametersMap.put(name,json.getString(Name));
parametersMap.put(start,json.getString(Start));
parametersMap.put(end,json.getString(end));
String report =C:\\My \\Path\\toReport\\report1.jrxml;
JasperReport Jasp = JasperCompileManager.compileReport(report);
JasperPrint JASP_PRINT = JasperFillManager.fillReport(Jasp,parametersMap);
JasperViewer.viewReport(JASP_PRINT);

更新:



这是我的Java代码

  public void start(Future< Void> startFuture){

String fileJSON =C: \\Users\\PathToMyFile\\data.txt;
String file =/ Test1 /:name;
路由器路由器= Router.router(vertx);
router.route(file).handler(routingContext - > {
HttpServerResponse response = routingContext.response();
response.setChunked(true);
routingContext.vertx( ).setTimer(1000,tid - > routingContext.response()。end());
});
vertx.createHttpServer()
.requestHandler(router :: accept)
.listen(8089,localhost,res - > {
if(res.succeeded() )
startFuture.complete();
else
startFuture.fail(res.cause());
});

vertx.fileSystem()。readFile(fileJSON,(AsyncResult< Buffer> result) - > {
if(result.succeeded()){
JsonObject json = result .result()。toJsonObject();
try {
Map parametersMap = new HashMap();
parametersMap.put(name,json.getString(name));
parametersMap.put(start,json.getString(start));
parametersMap.put(end,json.getString(end));
String report = C:\\Users\\pathToMyPackage\\report1.jrxml;
JasperReport Jasp = JasperCompileManager.compileReport(report);
JasperPrint JASP_PRINT = JasperFillManager.fillReport(Jasp,parametersMap );
JasperView er.viewReport(JASP_PRINT);

对于我的JRXML文件:

 < property name =ireport.zoomvalue =1.0/> 
< property name =ireport.xvalue =0/>
< property name =ireport.yvalue =0/>
< queryString language =json>
<![CDATA []]>
< / queryString>
< field name =nomclass =java.lang.String>
< fieldDescription><![CDATA [name]]>< / fieldDescription>
< / field>
< field name =dateDebutclass =java.lang.String>
< fieldDescription><![CDATA [start]]>< / fieldDescription>
< / field>
< background>
< band splitType =Stretch/>
< / background>
< detail>
< band height =125splitType =Stretch>
< textField>
< reportElement x =100y =0width =100height =30uuid =02b279da-3795-4655-8571-5a36a3ef378c/>
< textFieldExpression><![CDATA [$ F {name}]]>< / textFieldExpression>
< / textField>
< staticText>
< reportElement x =0y =0width =100height =30uuid =671e61ad-8d8f-48cb-969f-78c05a516398/>
< text><![CDATA [name]]>< / text>
< / staticText>
< textField>
< reportElement x =100y =30width =100height =30uuid =9d53f46f-a252-48b3-9213-8c3092c29f49/>
< textFieldExpression><![CDATA [$ F {start}]]>< / textFieldExpression>
< / textField>
< staticText>
< reportElement x =0y =30width =100height =30uuid =3b49affb-685a-4df2-a872-c0e6fdcab94b/>
< text><![CDATA [start]]>< / text>
< / staticText>
< / band>
< / detail>

我的JSON文件:

  {
名称:测试,
开始:16-06-2015,
结束:16-06- 2019
}


解决方案

你需要通过数据作为数据源,在json中这是通过将流传递给参数映射来完成的(因为您使用的是顶点,以String格式获取json,然后将其作为流传递)。



示例

  InputStream iostream = new ByteArrayInputStream (jsonString.getBytes(StandardCharsets.UTF_8)); 
parametersMap.put(JsonQueryExecuterFactory.JSON_INPUT_STREAM,iostream);

然后你可以定义与你的json和json(xpath)查询相关的字段


I'm beginner with JasperReports ( first try ) so, I set up my netbeans, and my first try using data from database was successful. But now I just want to create my report from json data using Jasper Reports in Netbeans And I think it's not like getting data from Database.

By the way I tried to edit my xml file and add this :

<textField>
            <reportElement x="0" y="31" width="555" height="20" uuid="9678a129-61e8-4034-ab8e-739ee5723c87"/>
            <textElement textAlignment="Right">
                <font size="12" isBold="true"/>
            </textElement>
            <textFieldExpression><![CDATA[$F{nom}]]></textFieldExpression>
        </textField>

But it gives me null in report.

Here is what I did in java side :

Map parametersMap = new HashMap();
parametersMap.put("name", json.getString("Name"));
parametersMap.put("start", json.getString("Start"));
parametersMap.put("end", json.getString("end"));
String report = "C:\\My\\Path\\toReport\\report1.jrxml";
JasperReport Jasp = JasperCompileManager.compileReport(report);
JasperPrint JASP_PRINT = JasperFillManager.fillReport(Jasp,parametersMap);
JasperViewer.viewReport(JASP_PRINT);

UPDATED:

Here is my Java code

public void start(Future<Void> startFuture){

 String fileJSON = "C:\\Users\\PathToMyFile\\data.txt";
     String file =  "/Test1/:name";
    Router router = Router.router(vertx);
       router.route(file).handler(routingContext -> {
        HttpServerResponse response = routingContext.response();
        response.setChunked(true);
        routingContext.vertx().setTimer(1000, tid -> routingContext.response().end());
    });
    vertx.createHttpServer()
            .requestHandler(router::accept)
            .listen(8089, "localhost", res -> {
                if (res.succeeded())
                    startFuture.complete();
                else
                    startFuture.fail(res.cause());
            });

    vertx.fileSystem().readFile(fileJSON, (AsyncResult<Buffer> result) -> {
        if (result.succeeded()) {                
                                JsonObject json = result.result().toJsonObject();
                             try{
                                Map parametersMap = new HashMap();  
                                parametersMap.put("name",json.getString("name"));
                                parametersMap.put("start",json.getString("start"));
                                parametersMap.put("end",json.getString("end"));
                                String report = "C:\\Users\\pathToMyPackage\\report1.jrxml";
                                JasperReport Jasp = JasperCompileManager.compileReport(report);
                                JasperPrint JASP_PRINT = JasperFillManager.fillReport(Jasp,parametersMap);
                                JasperViewer.viewReport(JASP_PRINT);   

And for my JRXML file :

<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="nom" class="java.lang.String">
    <fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<field name="dateDebut" class="java.lang.String">
    <fieldDescription><![CDATA[start]]></fieldDescription>
</field>
<background>
    <band splitType="Stretch"/>
</background>
<detail>
    <band height="125" splitType="Stretch">
        <textField>
            <reportElement x="100" y="0" width="100" height="30" uuid="02b279da-3795-4655-8571-5a36a3ef378c"/>
            <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
        </textField>
        <staticText>
            <reportElement x="0" y="0" width="100" height="30" uuid="671e61ad-8d8f-48cb-969f-78c05a516398"/>
            <text><![CDATA[name]]></text>
        </staticText>
        <textField>
            <reportElement x="100" y="30" width="100" height="30" uuid="9d53f46f-a252-48b3-9213-8c3092c29f49"/>
            <textFieldExpression><![CDATA[$F{start}]]></textFieldExpression>
        </textField>
        <staticText>
            <reportElement x="0" y="30" width="100" height="30" uuid="3b49affb-685a-4df2-a872-c0e6fdcab94b"/>
            <text><![CDATA[start]]></text>
        </staticText>
    </band>
</detail>

And my JSON file :

{
"Name": "Test",
"Start": "16-06-2015",
"End":"16-06-2019"
 }

解决方案

You need to pass the data as a datasource, in json this is done by passing the stream through the parameter map (since you are using vertex, get the json in String format and then pass it as a stream).

Example

InputStream iostream = new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8));
parametersMap.put(JsonQueryExecuterFactory.JSON_INPUT_STREAM, iostream);

Then you can define the fields related to your json and json (xpath) query

这篇关于使用JasperReports从json填写报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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