首页 > 解决方案 > 如何从简单的 Java 控制台应用程序将数据添加到 jasper 报告

问题描述

你好这是我第一次接触java和jasper。我通常有一个任务,我必须将 json 发送到 faas 函数并基于它生成一个 pdf 文件。不幸的是,在任务的一开始,我就遇到了将数据简单地静态添加到 jasper 文件的问题。请帮忙。我写了这样的东西:

package com.company;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String[] args) throws IOException, JRException {
        Map parameters = new HashMap();
        parameters.put("NAME", "Basic JasperReport");

        File reportFile  = new File ("C:/Users/SomeOne/Desktop/SomeOneTestFile.jasper"); 
        var jasperPrint = JasperFillManager.fillReport(reportFile.getAbsolutePath(),  parameters);//, new JREmptyDataSource());
beanColDataSource);
        JasperExportManager.exportReportToPdfFile(jasperPrint, "C:/Users/SomeOne/Desktop/Test/SomeOneTestFile.pdf");
    }
}

来自碧玉工作室的 xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.10.0.final using JasperReports Library version 6.10.0-unknown  -->
<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="BasicReport" pageWidth="595" pageHeight="842" whenNoDataType="NoPages" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30" uuid="7f398d81-11c1-4564-ad71-90436bb6fee5">
    <parameter name="Title" class="java.lang.String"/>
    <queryString>
        <![CDATA[select name, cost from product]]>
    </queryString>
    <field name="NAME" class="java.lang.String"/>
    <field name="COST" class="java.lang.Double"/>
    <title>
        <band height="50" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="200" height="50" uuid="94f89038-6ba7-4764-8776-0768a5bbed14"/>
                <textFieldExpression><![CDATA[$P{Title}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement x="180" y="0" width="180" height="20" uuid="29b288d7-e434-4860-a426-ace53ecbbe26"/>
                <textElement>
                    <font isUnderline="true"/>
                </textElement>
                <text><![CDATA[NAME]]></text>
            </staticText>
            <staticText>
                <reportElement x="360" y="0" width="180" height="20" uuid="4285064b-5e25-47b5-9441-5ce007180994"/>
                <textElement>
                    <font isUnderline="true"/>
                </textElement>
                <text><![CDATA[COST]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="180" y="0" width="180" height="20" uuid="c6152dbb-2cdd-45ad-94ff-828e20e56218"/>
                <textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
            </textField>
            <textField pattern="0.00">
                <reportElement x="360" y="0" width="180" height="20" uuid="96083012-54c2-496e-87c1-2d2bad7d973c"/>
                <textFieldExpression><![CDATA[$F{COST}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="15" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="40" height="15" uuid="3d5ffca0-e1c9-4d18-94a7-8a01fd29a0c5"/>
                <text><![CDATA[Page:]]></text>
            </staticText>
            <textField>
                <reportElement x="40" y="0" width="100" height="15" uuid="0d9f1097-9b03-4535-aa25-92423aaaa9bb"/>
                <textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
        </band>
    </pageFooter>
    <summary>
        <band splitType="Stretch"/>
    </summary>
</jasperReport>

我不知道为什么我得到一张白纸。我认为代码应该将数据添加到Name Field

标签: javajasper-reports

解决方案


推荐阅读