gpt4 book ai didi

java - 通过Java插入的jasper子报表为空

转载 作者:行者123 更新时间:2023-12-01 17:55:54 25 4
gpt4 key购买 nike

当通过 java 插入子报告时,我在显示子报告时遇到问题。我的目标是动态插入它们,所以我不知道事先会使用哪些子报表。母版页正确显示,而子报表为空白。
似乎我缺少与子报表的连接。也许有人能看出问题出在哪里?很抱歉没有提供最小的工作示例。
这是我设置参数并返回JasperPrint对象的方法:

private JasperPrint createReport(String masterReportFile, String subReportFile, JasperReportFileType fileType, String reportName) throws Exception {
JRBeanCollectionDataSource ticketsJRBean = new JRBeanCollectionDataSource(workStatList
.stream()
.map(this::generateNewJasperReportTicketObject)
.collect(Collectors.toList()));
ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
InputStream reportTemplate = new FileInputStream(context.getRealPath("") + JasperReportConstants.TemplateLocation.TEMPLATES_DEFAULT.getFullLocation(masterReportFile));
Map<TemplateLocation, JasperReport> subreportMap = provideRequiredReportsMap(subReportFile, context);
JasperReport jasperMasterReport = new JasperMasterReport(Optional.of(reportTemplate), reportName, subreportMap).createAndGetJasperReport();
Map<String, Object> paramsMaster = new HashMap<>();
setupMasterReportParameters(paramsMaster, fileType);
Map<String, Object> paramsSub = new HashMap<>();
paramsMaster.put(JasperTicketReport.PARAM_TICKET_SUBREPORT, subreportList.get(TemplateLocation.TICKETS));
paramsMaster.put(JasperTicketReport.PARAM_TICKET_DATASOURCE, ticketsJRBean);
paramsMaster.put(JasperTicketReport.PARAM_HEADER_ID, "HEADER");
return JasperFillManager.fillReport(jasperMasterReport, paramsMaster);
}

这是类方法,用于创建子报告的链接:

@Override
public JasperReport createAndGetJasperReport() throws Exception {
try {
initializeReport();
subReportMap.forEach((key, value) -> insertSubReport(key, value));
return JasperCompileManager.compileReport(jasperDesign);
} catch (JRException ex) {
LOGGER.log(Level.ERROR, ex.getMessage());
throw new Exception(ex.getMessage());
}
}
private void insertSubReport(TemplateLocation templateLocation, JasperReport report) {
try {
JRDesignBand subreportBand = new JRDesignBand();
subreportBand.setHeight(50);
JRDesignSubreport subreport = new JRDesignSubreport(jasperDesign);
subreport.setX(0);
subreport.setY(0);
subreport.setHeight(20);
subreport.setWidth(355);
JRDesignExpression expression = new JRDesignExpression();
JRDesignSubreportParameter jrDesignSubreportParameterDataSource = new JRDesignSubreportParameter();
JRDesignSubreportParameter jrDesignSubreportParameterHeaderId = new JRDesignSubreportParameter();
if (TemplateLocation.TICKETS.equals(templateLocation)) {
defineParameter(JRBeanCollectionDataSource.class, JasperTicketReport.PARAM_TICKET_DATASOURCE);
defineParameter(JasperReport.class, JasperTicketReport.PARAM_TICKET_SUBREPORT);
defineParameter(String.class, JasperTicketReport.PARAM_HEADER_ID);
expression.setText("$P{" + JasperTicketReport.PARAM_TICKET_SUBREPORT + "}");
subreport.setExpression(expression);
expression = new JRDesignExpression();
expression.setText("$P{" + JasperTicketReport.PARAM_HEADER_ID + "}");
jrDesignSubreportParameterHeaderId.setExpression(expression);
jrDesignSubreportParameterHeaderId.setName("ticketHeader");
subreport.addParameter(jrDesignSubreportParameterHeaderId);
expression = new JRDesignExpression();
expression.setText("$P{" + JasperTicketReport.PARAM_TICKET_DATASOURCE + "}");
jrDesignSubreportParameterDataSource.setExpression(expression);
jrDesignSubreportParameterDataSource.setName("ticketDataSource");
subreport.addParameter(jrDesignSubreportParameterDataSource);
}
subreportBand.addElement(subreport);
((JRDesignSection) jasperDesign.getDetailSection()).addBand(subreportBand);
} catch (JRException ex) {
LOGGER.log(Level.ERROR, ex.getMessage());
}
}

这是我的主 jrxml 文件:

    <?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.11.0.final using JasperReports Library version 6.11.0-0c4056ccaa4d25a5a8c45672d2f764ea3498bebb -->
<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="Process_A4_Landscape" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="43b18c10-7dae-49ef-ae13-0a6e88c9a593">
<parameter name="title" class="java.lang.String"/>
<parameter name="logoBinary" class="java.io.ByteArrayInputStream"/>
<parameter name="filter" class="java.lang.String"/>
<parameter name="disp" class="java.lang.String"/>
<field name="id" class="java.lang.Integer"/>
<field name="Title" class="java.lang.String">
<fieldDescription><![CDATA[$P{title}]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="60" splitType="Stretch">
<image>
<reportElement x="0" y="0" width="77" height="60" uuid="651ccc4b-e999-40fe-bcbf-1f25bcfc98af"/>
<imageExpression><![CDATA[$P{logoBinary}]]></imageExpression>
</image>
<textField>
<reportElement x="251" y="0" width="300" height="60" uuid="20b118bd-4ca6-4b6a-993d-19f038c89768">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
</reportElement>
<textElement textAlignment="Center">
<font size="18"/>
</textElement>
<textFieldExpression><![CDATA[$P{title}]]></textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band height="77" splitType="Stretch">
<textField>
<reportElement x="53" y="23" width="500" height="30" uuid="ece41608-e59d-4b05-b736-d4c84c420074">
<property name="com.jaspersoft.studio.unit.x" value="px"/>
<property name="com.jaspersoft.studio.unit.width" value="px"/>
</reportElement>
<textElement>
<font size="8"/>
</textElement>
<textFieldExpression><![CDATA[$P{filter}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="23" width="50" height="30" uuid="89f432c5-6e8e-4e56-9317-9131d59e4e31">
<property name="com.jaspersoft.studio.unit.x" value="px"/>
<property name="com.jaspersoft.studio.unit.width" value="px"/>
</reportElement>
<textElement>
<font size="8" isBold="true"/>
</textElement>
<text><![CDATA[FILTER]]></text>
</staticText>
</band>
</pageHeader>
<columnHeader>
<band height="50"/>
</columnHeader>
<detail>
<band height="280" splitType="Stretch"/>
</detail>
<pageFooter>
<band height="69" splitType="Stretch">
<textField pattern="dd/MM/yyyy HH:mm">
<reportElement x="50" y="23" width="100" height="30" uuid="6924da96-3fd4-455a-8361-bca736aad641"/>
<textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
</textField>
<textField>
<reportElement x="351" y="19" width="100" height="30" uuid="2143305d-6a29-4bc0-a689-1f359db58d49"/>
<textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
<summary>
<band height="46" splitType="Stretch"/>
</summary>
</jasperReport>

这是我的子报告:

 <?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.11.0.final using JasperReports Library version 6.11.0-0c4056ccaa4d25a5a8c45672d2f764ea3498bebb -->
<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="Process_A4_Landscape" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="43b18c10-7dae-49ef-ae13-0a6e88c9a593">
<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="TicketDataSet" uuid="af87285f-3ee2-454d-8102-313d6ab38914">
<parameter name="headerId" class="java.lang.String"/>
<parameter name="headerSubject" class="java.lang.String"/>
<parameter name="headerProject" class="java.lang.String"/>
<parameter name="headerType" class="java.lang.String"/>
<parameter name="headerDuration" class="java.lang.String"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="id" class="java.lang.Integer"/>
<field name="subject" class="java.lang.String"/>
<field name="fileName" class="java.lang.String"/>
<field name="typeName" class="java.lang.String"/>
<field name="workMinutes" class="java.lang.Integer"/>
</subDataset>
<parameter name="headerId" class="java.lang.String"/>
<parameter name="headerSubject" class="java.lang.String"/>
<parameter name="headerProject" class="java.lang.String"/>
<parameter name="headerType" class="java.lang.String"/>
<parameter name="headerDuration" class="java.lang.String"/>
<parameter name="ticketsDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
<field name="Title" class="java.lang.String">
<fieldDescription><![CDATA[TEST]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<columnHeader>
<band height="62" splitType="Stretch">
<componentElement>
<reportElement x="170" y="0" width="410" height="62" uuid="ea0d1bc7-20ee-4a73-8e20-82494215b940">
<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="TicketDataSet" uuid="061b061f-f5d5-4892-b3c4-e5b8d85f28a1">
<datasetParameter name="headerId">
<datasetParameterExpression><![CDATA[$P{headerId}]]></datasetParameterExpression>
</datasetParameter>
<dataSourceExpression><![CDATA[$P{ticketsDataSource}]]></dataSourceExpression>
</datasetRun>
<jr:column width="50" uuid="635ee962-de22-4d7e-a89f-395ad81bdb3c">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
<jr:tableHeader style="Table_TH" height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="50" height="30" uuid="c8b56f8c-593a-4c70-98bd-c20ceb88a528"/>
<textElement textAlignment="Left"/>
<textFieldExpression><![CDATA[$P{headerId}]]></textFieldExpression>
</textField>
</jr:tableHeader>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="50" height="30" uuid="32ef7b8c-84cb-4b59-85a9-decfdaf76967"/>
<textElement textAlignment="Left"/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</columnHeader>
<detail>
<band height="302" splitType="Stretch">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.spreadsheet.SpreadsheetLayout"/>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
</jasperReport>

最佳答案

所以我需要做两件事才能显示子报告:

  1. 根据 data67 的建议,我需要在子报表上设置 JREmptyDataSource():
    subreport.setDataSourceExpression(new JRDesignExpression("new net.sf.jasperreports.engine.JREmptyDataSource( )"));

  2. 我需要将子报表(在设置所有参数后)插入到主报表的列标题中:
    ((JRDesignBand) jasperDesign.getColumnHeader()).addElement(subreport);

关于java - 通过Java插入的jasper子报表为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60719511/

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