gpt4 book ai didi

java - 来自字符串的数据源

转载 作者:行者123 更新时间:2023-12-01 13:11:20 39 4
gpt4 key购买 nike

我想使用传递给 Java 的 args 字符串作为数据源来填充我的报告。我一直在尝试从字符串创建 JRXmlDataSource 以传递到 fillreport。我尝试过将字符串转换为文档,将字符串转换为文件流,然后直接加载字符串。我什至尝试创建一个文件并以这种方式加载它,但没有任何效果。任何帮助,将不胜感激。

主要方法:

public class Testing {

public static void main(String[] args) throws JRException {
try {
String xmlData = args[0];
JasperReport jasperReport = null;
String path = "C:/workspace/testProject/testing/sources/";
JasperPrint jasperPrint = null;

Document doc = convertStringToDocument(xmlData);
JRXmlDataSource xmlDataSource = new JRXmlDataSource(doc);

//Provide path for your JRXML template.
String templateName = path + "report2.jrxml";

//Provide path for your final pdf file.
String destinationFile = path + "ReportSQL.pdf";

//Compiling the template.
jasperReport = JasperCompileManager.compileReport(templateName);

//Sending a parameter with the logged in user name as value
HashMap parameters = new HashMap();

// Filling the report template with data
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, xmlDataSource);

//Sending a Complete print of the report.
JasperPrintManager.printReport(jasperPrint, true);

//Exporting it to an PDF
JasperExportManager.exportReportToPdfFile(jasperPrint, destinationFile);

} catch (Exception e) {
System.out.println(e);
}
}

private static Document convertStringToDocument(String xmlStr) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

源字符串:

 <?xml version="1.0" encoding="UTF-8"?>
<DATA>
<ROW>
<ID>1</ID>
<firstName>Testname</firstName>
<lastName>Testlast</lastName>
<code>12345</code>
</ROW>
<ROW>
<ID>2</ID>
<firstName>John</firstName>
<lastName>Doe</lastName>
<code>54321</code>
</ROW>
<ROW>
<ID>3</ID>
<firstName>Jane</firstName>
<lastName>Doe</lastName>
<code>22222</code>
</ROW>
<ROW>
<ID>4</ID>
<firstName>George</firstName>
<lastName>Washington</lastName>
<code>33333</code>
</ROW>
<ROW>
<ID>5</ID>
<firstName>John</firstName>
<lastName>Adams</lastName>
<code>44444</code>
</ROW>
<ROW>
<ID>6</ID>
<firstName>Thomas </firstName>
<lastName>Jefferson</lastName>
<code>55555</code>
</ROW>
<ROW>
<ID>7</ID>
<firstName>James</firstName>
<lastName>Madison</lastName>
<code>66666</code>
</ROW>
<ROW>
<ID>8</ID>
<firstName>James</firstName>
<lastName>Monroe</lastName>
<code>77777</code>
</ROW>
<ROW>
<ID>9</ID>
<firstName>John Quency</firstName>
<lastName>Adams</lastName>
<code>88888</code>
</ROW>
<ROW>
<ID>10</ID>
<firstName>Andrew</firstName>
<lastName>Jackson</lastName>
<code>99999</code>
</ROW>
<ROW>
<ID>11</ID>
<firstName>Martin Van</firstName>
<lastName>Burren</lastName>
<code>0</code>
</ROW>
</DATA>

Jasper XML---report2.jrxml:

     <?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="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5263a626-fdd2-43a7-b67e-ced19b4c240b">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<field name="firstName" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="lastName" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="code" class="java.lang.Integer">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<variable name="testout" class="java.lang.Integer">
<variableExpression><![CDATA[$V{testout}.valueOf($V{testout}.intValue() + 1 )]]></variableExpression>
<initialValueExpression><![CDATA[$V{testout}.intValue(0)]]></initialValueExpression>
</variable>
<variable name="variable1" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="71" splitType="Stretch">
<staticText>
<reportElement mode="Opaque" x="0" y="2" width="555" height="55" forecolor="#000000" backcolor="#6699FF" uuid="d2f1857a-a53f-4b65-8960-ff56856000c4"/>
<textElement textAlignment="Center">
<font fontName="SansSerif" size="40"/>
</textElement>
<text><![CDATA[People Test]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="16" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="33" splitType="Stretch">
<elementGroup>
<staticText>
<reportElement mode="Opaque" x="360" y="0" width="195" height="20" backcolor="#999999" uuid="2311af72-d724-4003-b5e4-151bc23e41a8"/>
<textElement textAlignment="Center">
<font size="14" isBold="true"/>
</textElement>
<text><![CDATA[Code]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="168" y="0" width="192" height="20" backcolor="#999999" uuid="0fc99eec-a7c3-468f-ba64-30f3140131b2"/>
<textElement textAlignment="Center">
<font size="14" isBold="true"/>
</textElement>
<text><![CDATA[Last]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="0" y="0" width="168" height="20" backcolor="#999999" uuid="b0a4ccab-fff0-4a77-ad52-1e4132a627e2"/>
<textElement textAlignment="Center">
<font size="14" isBold="true"/>
</textElement>
<text><![CDATA[First ]]></text>
</staticText>
</elementGroup>
</band>
</columnHeader>
<detail>
<band height="36" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="168" height="20" uuid="ca9be109-f4d6-48d6-988f-a5d78352e578"/>
<textElement textAlignment="Center"/>
<textFieldExpression><![CDATA[$F{firstName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="168" y="0" width="192" height="20" uuid="1255a5e6-59b1-4d49-92e0-c6b64a0cbf93"/>
<textElement textAlignment="Center"/>
<textFieldExpression><![CDATA[$F{lastName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="360" y="0" width="195" height="20" uuid="1099096e-17b7-480c-8e9c-ffb50669326a"/>
<textElement textAlignment="Center"/>
<textFieldExpression><![CDATA[$F{code}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="28" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="214" splitType="Stretch">
<image>
<reportElement mode="Opaque" x="194" y="0" width="166" height="178" backcolor="#000000" uuid="4299a501-2792-4c76-bc6d-506a480fa1a6"/>
<imageExpression><![CDATA["C:\\workspace\\testProject\\testing\\sources\\Turkish_Van_Cat.jpg"]]></imageExpression>
</image>
<textField>
<reportElement x="197" y="198" width="163" height="16" uuid="801deb03-5452-4bc6-935e-2c15a8977721"/>
<textElement textAlignment="Center"/>
<textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
<summary>
<band height="104" splitType="Stretch"/>
</summary>
</jasperReport>

最佳答案

您的 field 元素不包含 XML 路径表达式。试试这个:

<field name="firstName" class="java.lang.String">
<fieldDescription><![CDATA[DATA/ROW/firstName]]></fieldDescription>
</field>
<field name="lastName" class="java.lang.String">
<fieldDescription><![CDATA[DATA/ROW/lastName]]></fieldDescription>
</field>
<field name="code" class="java.lang.Integer">
<fieldDescription><![CDATA[DATA/ROW/code]]></fieldDescription>
</field>

此外,我有时会使用一些小技巧。围绕 JRXmlDataSource 创建一个包装类,在 Jasper 检索字段时转储字段内容:

package <your package here>;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
import net.sf.jasperreports.engine.data.JRXmlDataSource;

public class MyXmlDataSource extends JRXmlDataSource {

public MyXmlDataSource(String x, String y) throws JRException
{
super(x,y);
}

public Object getFieldValue(JRField jrField) throws JRException {
Object ret = super.getFieldValue(jrField);
System.out.println(ret);
return ret;
}
}

然后,不实例化 JRXmlDataSource,而是实例化 MyXmlDataSource。这将使您知道 Jasper 正在从您的 XML 中检索哪些元素。

关于java - 来自字符串的数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22841901/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com