背景
最近在公司做打印工作台的功能模块,前期基本都是打印一些条形码、文字、logo等,做到中期,需要显示表格,这可让我犯了愁,没搞过呀,先去看下官方文档。好,无获而归。百度、google很多博文也都写的不清不楚,然后只能随便看一篇博文来作为参考。一下踩了不少坑,但不一一阐述。直说步骤,方便以后回来查看。
准备工作
工具:Jaspersoft Studio
1.首先是制作相关的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="产品说明书" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="1eeecacf-d9a5-41f7-b976-f1d25c672bfe">
<property name="com.jaspersoft.studio.unit." value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<import value="net.sf.jasperreports.engine.*"/>
<import value="java.util.*"/>
<import value="net.sf.jasperreports.engine.data.*"/>
<style name="table">
<box>
<pen lineWidth="1.0" lineColor="#000000"/>
</box>
</style>
<style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
<conditionalStyle>
<conditionExpression><![CDATA[Boolean.valueOf($V{REPORT_COUNT}.intValue() % 2 == 0)]]></conditionExpression>
<style backcolor="#FFFFFF"/>
</conditionalStyle>
</style>
<subDataset name="DS" whenResourceMissingType="Empty" uuid="f5d7cb82-7ed1-4036-afc6-b465b06cdb23">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<field name="productCode" class="java.lang.String"/>
<field name="propertyCode" class="java.lang.String"/>
<field name="productName" class="java.lang.String"/>
<field name="quantity" class="java.lang.String"/>
<field name="rack" class="java.lang.String"/>
</subDataset>
<field name="area" class="java.lang.String"/>
<field name="date" class="java.lang.String"/>
<field name="barCode" class="java.lang.String"/>
<field name="productList" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="795" splitType="Stretch">
<textField>
<reportElement x="10" y="10" width="149" height="20" uuid="3c381743-52f1-41e3-ab6b-0a5e45c17b6f"/>
<textElement>
<font size="12" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
</textField>
<componentElement>
<reportElement x="270" y="0" width="283" height="50" uuid="f9a57433-b8b0-43cf-a38b-13b605e550ed"/>
<jr:barbecue xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" type="Code128" drawText="false" checksumRequired="false" barHeight="50">
<jr:codeExpression><![CDATA[$F{barCode}]]></jr:codeExpression>
</jr:barbecue>
</componentElement>
<textField>
<reportElement x="280" y="50" width="263" height="20" uuid="59e3b98d-26bd-4692-8b89-c35fd174efa2"/>
<textElement textAlignment="Center">
<font size="12" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{barCode}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="10" y="40" width="120" height="40" uuid="b75fcf87-d52d-4030-bcc9-704472c59ada"/>
<box>
<pen lineStyle="Solid"/>
</box>
<textElement textAlignment="Center">
<font size="20" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{area}]]></textFieldExpression>
</textField>
<componentElement>
<reportElement key="table" x="0" y="110" width="553" height="570" isRemoveLineWhenBlank="true" uuid="c07dfc87-757a-4877-9d6e-8595ceda0e96">
<property name="com.jaspersoft.studio.components.autoresize.proportional" value="true"/>
<property name="net.sf.jasperreports.export.headertoolbar.table.name" value=""/>
</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="DS" uuid="b0982f8c-a378-4312-a9d6-82539b371998">
<dataSourceExpression><![CDATA[$F{productList}]]></dataSourceExpression>
</datasetRun>
<jr:column width="110" uuid="d47d54ba-8219-4c38-88e5-8675ab66e7e4">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
<jr:columnHeader style="table_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="110" height="30" uuid="f4ef5229-262b-4d79-bac2-5106d03b03d9"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[SKU]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="20" rowSpan="1">
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="110" height="20" uuid="773f22a8-b3c3-4b1e-a546-e873d485c20a"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{productCode}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="132" uuid="488c1315-2cd6-4503-b84a-31994e909b28">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
<jr:columnHeader style="table_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="132" height="30" uuid="5a45a03e-5bbe-4096-a7ec-a6a9d21dc71e"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[POA]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="20" rowSpan="1">
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="132" height="20" uuid="1c76a435-4a38-4b58-91f9-e09459c8ada1"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{propertyCode}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="106" uuid="ab571d38-85ca-4cb2-8ed5-fdac41d9541a">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column3"/>
<jr:columnHeader style="table_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="106" height="30" uuid="b4b5620b-a426-486f-95d5-62c57e10640e"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Name]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="20" rowSpan="1">
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="106" height="20" uuid="b5d58e46-2ecd-4f96-a327-6c172b1d4870"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{productName}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="105" uuid="4329a7d4-0a8f-4fac-93ff-ce3fe77d1e0d">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column4"/>
<jr:columnHeader style="table_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="105" height="30" uuid="c689d108-5c21-41e1-89d2-0c50038f9de5"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Quantity]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="20">
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="105" height="20" uuid="cdf2822f-7576-4767-bf99-4377ff60d00c"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="100" uuid="45da0fa7-b547-4982-a95a-0724e980df70">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column5"/>
<jr:columnHeader style="table_CH" height="30" rowSpan="1">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="5ebc46f6-f689-4ec5-9ed6-3509aad4011f"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Rack]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="20">
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="100" height="20" uuid="0d23fc22-3b47-4ed1-8890-c46ea666ca52"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{rack}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
</jasperReport>
可能有点乱,看一下效果图。
细节部分不描述了,直说表格设计这部分。
首先是设计表格,对应行头和cell的内容。如下图.
2.为表格设置数据源,数据源名字为DS,而数据源是使用javabean传进来的。对应字段productList,然后新建的数据源使用传递进来的数据。如图.
3.为②中新建的数据源设置要使用的字段Fields,如图.
4.至此,模板设计完毕,接下来是java代码封装数据传递给模板。这里主要是使用到了
使用这个类来装载我们的数据,注意它的构造函数只能是列表。所以我们把表格数据包装到List里面,最后再调其构造函数即可。话不多说,上代码:
@Data
public class QualityInspectionProductDataSource implements Serializable {
private String barCode;
private String date;
private String area;
private JRBeanCollectionDataSource productList;
}
@Data
public class QualityInspectionProduct implements Serializable {
private Integer storageId;
private String qualityInspectionCode;
private String productCode;
private String propertyCode;
private String productName;
private String quantity;
private String area;
private String rack;
}
QualityInspectionProductDataSource dataSource = new QualityInspectionProductDataSource();
dataSource.setBarCode(qualityInspectionCode);
dataSource.setDate(nowStr);
dataSource.setArea(qualityInspectionProducts.get(0).getArea());
dataSource.setProductList(new JRBeanCollectionDataSource(qualityInspectionProducts));
由于productList是一个集合,不能以list的方式传递进去,不然jasper无法解析,同理需要将其转换为JRBeanCollectionDataSource传递进去。最后就是将dataSource作为数据源传递解析,最后的实现步骤为
//根据参数填充模板
@Cleanup InputStream is = new ByteArrayInputStream(templateFile);
JasperDesign jasperDesign;
jasperDesign = JRXmlLoader.load(is);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource((Collection<?>) params.remove("_list_"));
//导出结果文件
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
return JasperExportManager.exportReportToPdf(jasperPrint);