【问题标题】:Filling reports from json using JasperReports [duplicate]使用 JasperReports 从 json 填充报告 [重复]
【发布时间】:2016-04-06 09:46:20
【问题描述】:

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

顺便说一句,我尝试编辑我的 xml 文件并添加以下内容:

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

但它在报告中给了我空值。

这是我在 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 = 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);   

对于我的 JRXML 文件:

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

还有我的 JSON 文件:

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

【问题讨论】:

  • 使用示例:JSON Data Source Overview
  • 我已经发布了一个副本,展示了如何将 json 数据源传递给报告,基本上你不通过作为数据源的参数看到副本(如果你解决了它,你接受副本)
  • 感谢 Alex 提供的示例,但我仍有一些问题
  • Petter 我使用 vertx 所以我必须从缓冲区读取并转换为 Json 文件,我可以从 Jasper Docs 中看到他们实现了没有第三个参数的填充方法( fillReport(java.io.InputStream inputStream, java .util.Map parameters) ) 我尝试使用我的 java 代码从您的 XML 文件创建报告,但它给了我 null、null、null
  • @zackzulg 你应该发布完整的示例来检查:简单的 json、简单的 jrxml、简单的 Java 代码生成报告。

标签: java json jasper-reports datasource


【解决方案1】:

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

示例

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多