1 回答
TA贡献1966条经验 获得超4个赞
我在这个堆栈溢出问题中找到了解决方案,答案中突出显示的步骤是:
将数据源作为参数从服务器发送,并使用其他数据源(可以是空)填充报表。
在 JRBeanCollectionDataSource 类型的报表中声明一个新参数
将表数据源设置为使用 $P{DS1} 参数。
...
此外,我修改了我的报告文件字段声明,并删除了那里的所有jasper studio属性。
执行此步骤并修改我的报表文件和控制器后,它起作用了。这是我的新控制器方法:
public void report(HttpServletResponse response) throws Exception {
response.setContentType("text/html");
List data = userReportService.report();
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(data);
Map parameters = new HashMap();
parameters.put("test", data.get(0));
parameters.put("DS1", dataSource);
InputStream inputStream = this.getClass().getResourceAsStream("/reports/users_report.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
HtmlExporter exporter = new HtmlExporter(DefaultJasperReportsContext.getInstance());
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getWriter()));
exporter.exportReport();
}
我的新 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="Sample_Blue_Table_Based" pageWidth="842" pageHeight="595" whenNoDataType="AllSectionsNoDetail" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d4a1d5eb-82dd-412f-a620-202e4bdf9dba">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<style name="Table">
<box>
<pen lineWidth="1.0" lineColor="#000000"/>
<topPen lineWidth="1.0" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineColor="#000000"/>
</box>
</style>
<style name="Table_TH" mode="Transparent" 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>
<style name="Table_CH" mode="Transparent" forecolor="#B89F7D" backcolor="#70A9C6">
<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="Transparent" 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="tableDataset" uuid="d126342f-2c37-4323-99a6-4d0a9542aa8a">
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="id" class="java.lang.Integer"/>
<field name="firstName" class="java.lang.String"/>
<field name="lastName" class="java.lang.String"/>
<field name="username" class="java.lang.String"/>
<field name="email" class="java.lang.String"/>
<field name="status" class="java.lang.Boolean"/>
<field name="created_at" class="java.sql.Timestamp"/>
</subDataset>
<parameter name="DS1" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
<title>
<band height="72">
<frame>
<reportElement mode="Opaque" x="-20" y="-20" width="842" height="92" backcolor="#4AC416" uuid="6321f5e3-86eb-444f-acc1-8daaaa0d6c60"/>
<staticText>
<reportElement x="20" y="20" width="320" height="43" forecolor="#FFFFFF" uuid="9e48917a-7b59-4ffd-baad-725f45563194"/>
<textElement>
<font size="34" isBold="true"/>
</textElement>
<text><![CDATA[Admin App Users]]></text>
</staticText>
<staticText>
<reportElement x="650" y="46" width="180" height="20" forecolor="#FFFFFF" uuid="0a399baa-bad3-4b1a-9fe3-bb1c563e6a27"/>
<textElement textAlignment="Right">
<font size="14" isBold="false"/>
</textElement>
<text><![CDATA[Application Users Report]]></text>
</staticText>
</frame>
</band>
</title>
<pageHeader>
<band height="13"/>
</pageHeader>
<pageFooter>
<band height="17">
<textField>
<reportElement mode="Opaque" x="0" y="4" width="760" height="13" backcolor="#E6E6E6" uuid="fed27553-a6c4-4a8a-9056-2f6f2589a3e5"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
</textField>
<textField evaluationTime="Report">
<reportElement mode="Opaque" x="762" y="4" width="40" height="13" backcolor="#E6E6E6" uuid="9f5ad531-d413-49c9-874b-8b99e8150fa6"/>
<textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
<textField pattern="EEEEE dd MMMMM yyyy">
<reportElement x="0" y="4" width="100" height="13" uuid="fc593335-5cbf-4caf-97b3-ff897c676bde"/>
<textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
</textField>
</band>
</pageFooter>
<summary>
<band height="200" splitType="Stretch">
<property name="local_mesure_unitheight" value="pixel"/>
<componentElement>
<reportElement x="0" y="0" width="806" height="200" uuid="01fed4d0-1b62-44ff-9fa5-e47ff6987d68">
<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="tableDataset" uuid="ee7a04e1-1abd-4fc2-baf5-52183f1eaa43">
<datasetParameter name="REPORT_DATA_SOURCE">
<datasetParameterExpression><![CDATA[$P{DS1}]]></datasetParameterExpression>
</datasetParameter>
</datasetRun>
<jr:column width="60" uuid="99ec822c-fb05-4f72-abd4-5f009620a549">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
<jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>
<jr:columnHeader style="Table_CH" height="30" rowSpan="1">
<staticText>
<reportElement mode="Transparent" x="0" y="0" width="60" height="30" forecolor="#006699" backcolor="#E6E6E6" uuid="06b3eded-dc90-4b57-9d0e-c51c6f342892"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="14" isBold="true"/>
</textElement>
<text><![CDATA[ID]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement key="" x="0" y="0" width="60" height="30" uuid="19c36999-b01b-4bbd-a41e-c53ae0c80afd"/>
<box leftPadding="2"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial" size="14"/>
</textElement>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="140" uuid="2483833e-c55e-4a69-8868-9b08db7559c0">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
<jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>
<jr:columnHeader style="Table_CH" height="30" rowSpan="1">
<staticText>
<reportElement mode="Transparent" x="0" y="0" width="140" height="30" forecolor="#006699" backcolor="#E6E6E6" uuid="9c786800-8901-47ae-bd4a-3c01204abd4d"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="14" isBold="true"/>
</textElement>
<text><![CDATA[First Name]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement key="" x="0" y="0" width="140" height="30" uuid="d7b6cf06-c37f-4ec1-a519-3e7fe2d7be8b"/>
<box leftPadding="2"/>
<textElement verticalAlignment="Middle">
<font fontName="Arial" size="14"/>
</textElement>
<textFieldExpression><![CDATA[$F{firstName}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="140" uuid="8d185e10-164e-420a-8a7f-d159ff9cb691">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column3"/>
<jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>
<jr:columnHeader style="Table_CH" height="30" rowSpan="1">
<staticText>
<reportElement mode="Transparent" x="0" y="0" width="140" height="30" forecolor="#006699" backcolor="#E6E6E6" uuid="946eda2c-149c-4eb4-a929-cca937fb6393"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="14" isBold="true"/>
</textElement>
<text><![CDATA[Last Name]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement key="" x="0" y="0" width="140" height="30" uuid="09d3f107-6c04-4587-bf7a-91e871ce4f3b"/>
<box leftPadding="2"/>
<textElement verticalAlignment="Middle">
<font fontName="Arial" size="14"/>
</textElement>
<textFieldExpression><![CDATA[$F{lastName}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="130" uuid="395a497b-e0fc-4850-9f10-e41d4fd41035">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column4"/>
<jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>
<jr:columnHeader style="Table_CH" height="30" rowSpan="1">
<staticText>
<reportElement mode="Transparent" x="0" y="0" width="130" height="30" forecolor="#006699" backcolor="#E6E6E6" uuid="1912c136-2a05-4089-ae91-54a5d815bc92"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="14" isBold="true"/>
</textElement>
<text><![CDATA[Username]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement key="" x="0" y="0" width="130" height="30" uuid="cb9efd41-06b7-4059-a6ec-0fb35ccdbbc2"/>
<box leftPadding="2"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font fontName="Arial" size="14"/>
</textElement>
<textFieldExpression><![CDATA[$F{username}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="160" uuid="3c37b453-c14e-41af-af30-696eac992472">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column5"/>
<jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>
<jr:columnHeader style="Table_CH" height="30" rowSpan="1">
<staticText>
<reportElement mode="Transparent" x="0" y="0" width="160" height="30" forecolor="#006699" backcolor="#E6E6E6" uuid="58e2962c-e1f6-4649-8bd3-3e410fa58674"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="14" isBold="true"/>
</textElement>
<text><![CDATA[Email]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement key="" x="0" y="0" width="160" height="30" uuid="702a303c-9c29-4119-8156-8eb9bac28b7d"/>
<box leftPadding="2"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font fontName="Arial" size="14"/>
</textElement>
<textFieldExpression><![CDATA[$F{email}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="50" uuid="5a8d7002-6adc-4cff-9e7e-132ed9316b8e">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column6"/>
<jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>
<jr:columnHeader style="Table_CH" height="30" rowSpan="1">
<staticText>
<reportElement mode="Transparent" x="0" y="0" width="50" height="30" forecolor="#006699" backcolor="#E6E6E6" uuid="9ec69fab-1e14-49cf-ab25-1e8b037da361"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="14" isBold="true"/>
</textElement>
<text><![CDATA[A]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement key="" x="0" y="0" width="50" height="30" uuid="abfefc2d-93c3-4d65-afdc-5e9304224a32"/>
<box leftPadding="2"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial" size="14"/>
</textElement>
<textFieldExpression><![CDATA[$F{status}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="120" uuid="1af5f085-9784-49d9-b3b8-17cf34f4ac84">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column7"/>
<jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>
<jr:columnHeader style="Table_CH" height="30" rowSpan="1">
<staticText>
<reportElement mode="Transparent" x="0" y="0" width="120" height="30" forecolor="#006699" backcolor="#E6E6E6" uuid="fd3401b4-a35c-435b-9c9c-9ba9840db3b8"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="14" isBold="true"/>
</textElement>
<text><![CDATA[Created At]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement key="" x="0" y="0" width="120" height="30" uuid="9147c910-e8b4-4a79-8225-ea490c1de8a9"/>
<box leftPadding="2"/>
<textElement verticalAlignment="Middle">
<font fontName="Arial" size="14"/>
</textElement>
<textFieldExpression><![CDATA[$F{created_at}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</summary>
</jasperReport>
我希望这能为像我这样的新手节省一些头痛
添加回答
举报