【发布时间】:2018-07-16 19:27:04
【问题描述】:
我无法将数据放入表中。我使用的是第 14 章中的示例,只是我使用 Collection 而不是报告数据源。使用 JRBeanCollectionDataSource 中的 Collection 调用 JasperFillManager.fillReport() 不会显示任何数据,但会为 Collection 的每个元素创建一个空白页面。
在this example 之后,我已切换到在参数中发送数据。这在报告的第 2 页上提供了一个空表(示例本身有效)。
我的代码如下。
IntelliJ 抱怨 JasperSoft Studio 生成的 jrxml 无效。它说元素不能在它所在的位置。也许这有什么 处理它。
如何将数据放入 JasperReports 中的表格中?
Java
public class JasperReportsLineItemTableExample {
public static Logger logger = LoggerFactory.getLogger(JasperReportsLineItemTableExample.class);
public static void main(String[] args) {
String reportJrxmlFile = "LineItems_Table_A4.jrxml";
String outputFile = "LineItemsTableExample.pdf";
List<LineItem> items = LineItemsUtil.createItems(10);
logger.info(items.size() + " items");
JRBeanCollectionDataSource itemsJRBean = new JRBeanCollectionDataSource(items);
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("LineItemDataSource", itemsJRBean);
try {
JasperReport jasperReport = JasperCompileManager.compileReport(reportJrxmlFile);
// JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, itemsJRBean);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
try (OutputStream outputStream = new FileOutputStream(new File(outputFile))) {
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
System.out.println("File Generated");
} catch (IOException | JRException ex) {
ex.printStackTrace();
}
} catch (JRException ex) {
ex.printStackTrace();
}
}
}
JRXML
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<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="LineItems_Table_A4" pageWidth="595" pageHeight="842" whenNoDataType="NoDataSection" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="565ea393-c6d0-4128-8564-05701d3de374">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<subDataset name="Dataset1" uuid="57478514-4d8c-49d7-a6e5-8566d1ec7332">
<queryString>
<![CDATA[]]>
</queryString>
<field name="itemNumber" class="java.lang.String">
<fieldDescription><![CDATA[itemNumber]]></fieldDescription>
</field>
<field name="quantity" class="java.lang.Integer">
<fieldDescription><![CDATA[quantity]]></fieldDescription>
</field>
<field name="unitCost" class="java.lang.Float">
<fieldDescription><![CDATA[unitCost]]></fieldDescription>
</field>
<field name="totalCost" class="java.lang.Float">
<fieldDescription><![CDATA[totalCost]]></fieldDescription>
</field>
</subDataset>
<parameter name="LineItemDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
<queryString>
<![CDATA[]]>
</queryString>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement x="260" y="34" width="100" height="30" uuid="096a5061-5710-4597-9ce7-e4a7edbfe155"/>
<text><![CDATA[LineItems Table]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="357" splitType="Stretch">
<componentElement>
<reportElement positionType="FixRelativeToBottom" x="60" y="110" width="440" height="200" uuid="657ac0a8-31c5-4336-8e8d-fdec5dc92bd6">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
<property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
<property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
<property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
</reportElement>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="Dataset1" uuid="ac1740d4-8a9f-409d-979c-ad8788815312">
<dataSourceExpression><![CDATA[$P{LineItemDataSource} ]]></dataSourceExpression>
</datasetRun>
<jr:column width="100" uuid="a4de9f35-c7a8-4d2d-8ea1-339144d7d3d4">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="7ea912f2-e802-4b7a-9ccb-3e7b3762f862"/>
<text><![CDATA[itemNumber]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="6e4662e4-9421-4328-8d00-766416414c6d"/>
<textFieldExpression><![CDATA[$F{itemNumber}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="100" uuid="d10a0d67-02ea-4153-a799-6b4dbdd69200">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="4ba9a6bc-e6ed-477e-ae2f-0d91b27cd42f"/>
<text><![CDATA[quantity]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="366e5229-7cb8-441c-9a2a-82535212f06c"/>
<textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="100" uuid="266fdaed-4f71-43d5-8515-89604973dcd9">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="50" height="30" uuid="c8bbc714-10b6-4305-bf55-9feb87c32498"/>
<text><![CDATA[unitCost]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="9db777d5-428c-4387-8e9b-02aa4422407e"/>
<textFieldExpression><![CDATA[$F{unitCost}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="100" uuid="b70c951b-951d-443f-9c21-8acf8b025f64">
<jr:tableHeader style="Table_TH" height="30"/>
<jr:tableFooter style="Table_TH" height="30"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="b9b914be-ab50-4e2e-bf52-fca2a891a7de"/>
<text><![CDATA[total Cost]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="071819a9-ab97-41df-b2b7-b8e9783dfb4d"/>
<textFieldExpression><![CDATA[$F{totalCost}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
【问题讨论】:
-
每次编译
.jrxml文件通常没有任何好处。改为编译、加载和填充.jasper文件。 (这可能不会解决问题,但会提高性能。)另外,考虑使用继承样式来消除线宽和边框周围的一些重复。 -
我将其保留为 jrxml,因为我对 jrxml 进行了一些手动编辑以使用参数而不是数据源,并进行了一些其他更改以使其工作。如果我无法将数据放入表中,那么优化这个是没有意义的。
标签: java jasper-reports