且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用JasperReports从json填写报告

更新时间:2023-02-15 14:35:52

你需要通过数据作为数据源,在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