jasperReport - 多Table的PDF中文导出

 项目中实际需求:

1、多个不定行的表格。

2、嵌入可变图片

3、PDF导出为中文。

基本知识

1.     下载地址:https://community.jaspersoft.com/community-download

目前最新的为:TIB_js-studiocomm_6.11.0_windows_x86_64.exe,

实际使用的是TIB_js-studiocomm_6.9.0_windows_x86_64.exe

2.     网上的关于Jasperreports+jaspersoft studio学习教程,写的不错。

a)     JasperReports简单示例

b)     使用Parameters属性传入Map数据源填充报表

c)     解决打印pdf中文不显示问题

d)     JDBC向导作为数据源填充数据

e)     JavaBean作为数据源填充数据

f)      分组显示数据

g)     Table组件的使用

h)     子报表Subreport(父子报表互相传值)

i)      报表分页和大量数据内存处理

j)      多数据源的Chart图表

k)     JasperReportUtils

l)     JasperReport 中踩过的坑

3.     项目在springMvc中使用,需要的Jar如下:

1) itext-2.1.7.jar

2)  itext-rtf-2.1.7.jar

3)  iTextAsian.jar

4)  jasperreports-6.9.0.jar

5)  jasperreports-chart-customizers-6.9.0.jar

6)  jasperreports-chart-themes-6.9.0.jar

7)  jasperreports-fonts-6.9.0.jar

8)  jasperreports-functions-6.9.0.jar

9)  jasperreports-metadata-6.9.0.jar

10)commons-digester-2.1.jar

11)commons-beanutils-1.8.3.jar

4.     关于PDF解决中文字体的。这篇文章比较靠谱。实际使用好使。https://blog.csdn.net/DR_LF/article/details/78989470

项目实战

1、报表模板设计

      1)多个不定行的表格。使用两个Table来实现。具体实现方式参考上面的链接【Table组件的使用

 需要注意的是为了保证第二个Table的位置,在第一个Table的最后行下边。需要使用多个Detail区域的方式。

       2)嵌入可变图片,就比较简单了。直接给图片的expression传递image的路径就可以了,可以是本地路径,可以是url。

       3)PDF导出为中文。稍微复杂一些。

       在jasperreports-fonts-6.9.0.jar已经集成了一些字体,但是这些字体对中文支持不够。所以需要修改jasperreports-fonts-6.9.0.jar,在net\sf\jasperreports\fonts\fonts.xml中增加了【宋体】的支持,并把SimSun.ttf放在相应的位置。

	<fontFamily name="宋体">
		<normal>net/sf/jasperreports/fonts/dejavu/SimSun.ttf</normal>
		<bold>net/sf/jasperreports/fonts/dejavu/SimSun.ttf</bold>
		<italic>net/sf/jasperreports/fonts/dejavu/SimSun.ttf</italic>
		<boldItalic>net/sf/jasperreports/fonts/dejavu/SimSun.ttf</boldItalic>
		<pdfEncoding>Identity-H</pdfEncoding>
		<pdfEmbedded>true</pdfEmbedded>
		<exportFonts>
			<export key="net.sf.jasperreports.html">'宋体', Arial, Helvetica, sans-serif</export>
			<export key="net.sf.jasperreports.xhtml">'宋体', Arial, Helvetica, sans-serif</export>
		</exportFonts>
	</fontFamily>

      在设计报表模板时,字体统一选择宋体。

      修改后的Jar下载地址:https://download.csdn.net/download/qgbihc/12190031

最终的模板内容如下:test.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.9.0.final using JasperReports Library version 6.9.0-cb8f9004be492ccc537180b49c026951f4220bf3  -->
<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="05_contract" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="95f030bc-71a0-4115-a89c-2900bc82939b">
	<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>
	<style name="Table 1_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 1_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 1_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="f8c4dc72-2119-4bd6-bb2e-ce54bb0b0501">
		<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
		<queryString>
			<![CDATA[]]>
		</queryString>
		<field name="field1" class="java.lang.String"/>
		<field name="field2" class="java.lang.String"/>
		<field name="field3" class="java.lang.String"/>
		<field name="field4" class="java.lang.String"/>
		<field name="field5" class="java.lang.String"/>
	</subDataset>
	<queryString>
		<![CDATA[]]>
	</queryString>
	<field name="tableData" class="java.util.List"/>
	<field name="makeUser" class="java.lang.String"/>
	<field name="docNo" class="java.lang.String"/>
	<field name="totalFee" class="java.lang.String"/>
	<field name="tableData2" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
	<field name="imageUrl" class="java.lang.String"/>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band height="65" splitType="Stretch">
			<property name="com.jaspersoft.studio.unit.height" value="px"/>
			<staticText>
				<reportElement x="180" y="16" width="200" height="30" uuid="1846fb93-6cc1-4cc5-a806-0880d0d38d39"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font fontName="宋体" size="18" isBold="true"/>
				</textElement>
				<text><![CDATA[测试JasperReport]]></text>
			</staticText>
		</band>
	</title>
	<detail>
		<band height="130" splitType="Stretch">
			<property name="com.jaspersoft.studio.unit.height" value="px"/>
			<staticText>
				<reportElement x="0" y="0" width="100" height="30" uuid="7e47f290-78ea-422b-9c50-b6efcfced2f6"/>
				<box>
					<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
					<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
					<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
					<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font fontName="宋体" isItalic="true" isUnderline="true"/>
				</textElement>
				<text><![CDATA[定作人:]]></text>
			</staticText>
			<textField>
				<reportElement x="100" y="0" width="175" height="30" uuid="53f31274-11ce-4f79-85a1-d2c8c8bd0c29">
					<property name="com.jaspersoft.studio.unit.width" value="px"/>
				</reportElement>
				<box>
					<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
					<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
					<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
					<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
				</box>
				<textElement textAlignment="Left" verticalAlignment="Middle">
					<font fontName="宋体"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{makeUser}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement x="275" y="0" width="100" height="30" uuid="93548d1e-6d89-4da1-b186-551a878cfd9d">
					<property name="com.jaspersoft.studio.unit.x" value="px"/>
				</reportElement>
				<box>
					<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
					<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
					<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
					<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
				</box>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font fontName="宋体"/>
				</textElement>
				<text><![CDATA[文档编号:]]></text>
			</staticText>
			<textField>
				<reportElement x="375" y="0" width="175" height="30" uuid="8e3dacec-7c6b-4418-9562-ea79f7400ca5">
					<property name="com.jaspersoft.studio.unit.width" value="px"/>
				</reportElement>
				<box>
					<topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
					<leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
					<bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
					<rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/>
				</box>
				<textElement textAlignment="Left" verticalAlignment="Middle">
					<font fontName="宋体"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{docNo}]]></textFieldExpression>
			</textField>
			<componentElement>
				<reportElement stretchType="ContainerHeight" x="0" y="40" width="555" height="90" uuid="e7645099-9ad8-4ede-8e56-6c6177175dcc">
					<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" whenNoDataType="AllSectionsNoDetail">
					<datasetRun subDataset="Dataset1" uuid="c12aac31-9653-403b-b6fa-9de2956e72ac">
						<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{tableData})]]></dataSourceExpression>
					</datasetRun>
					<jr:column width="189" uuid="961a370c-1c60-4163-9c1e-14bcade3fcfc">
						<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
						<jr:columnHeader style="Table_CH" height="60" rowSpan="2">
							<staticText>
								<reportElement x="0" y="0" width="189" height="60" uuid="42320b63-e9f5-4c4d-a5a8-518a7c4d8e07"/>
								<textElement textAlignment="Center" verticalAlignment="Middle">
									<font fontName="宋体"/>
								</textElement>
								<text><![CDATA[项目名]]></text>
							</staticText>
						</jr:columnHeader>
						<jr:detailCell style="Table_TD" height="30">
							<textField>
								<reportElement x="0" y="0" width="189" height="30" uuid="1c414760-9968-4532-af1b-c375edbe026f"/>
								<textElement verticalAlignment="Middle">
									<font fontName="宋体"/>
								</textElement>
								<textFieldExpression><![CDATA[$F{field1}]]></textFieldExpression>
							</textField>
						</jr:detailCell>
					</jr:column>
					<jr:column width="126" uuid="34859f15-8749-4752-968a-5a4d963395d8">
						<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
						<jr:columnHeader style="Table_CH" height="60" rowSpan="2">
							<staticText>
								<reportElement x="0" y="0" width="126" height="60" uuid="7acf1a02-e345-4a4c-8efe-548c83d5aa63"/>
								<textElement textAlignment="Center" verticalAlignment="Middle">
									<font fontName="宋体"/>
								</textElement>
								<text><![CDATA[计量单位*]]></text>
							</staticText>
						</jr:columnHeader>
						<jr:detailCell style="Table_TD" height="30">
							<textField>
								<reportElement x="0" y="0" width="126" height="30" uuid="a7599607-0175-4fab-b001-c791aaed36c9"/>
								<textElement textAlignment="Center" verticalAlignment="Middle">
									<font fontName="宋体"/>
								</textElement>
								<textFieldExpression><![CDATA[$F{field2}]]></textFieldExpression>
							</textField>
						</jr:detailCell>
					</jr:column>
					<jr:column width="94" uuid="1acf7fb4-5f5c-4be5-9280-0e2f5168d3b1">
						<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column3"/>
						<jr:columnHeader style="Table_CH" height="60" rowSpan="2">
							<staticText>
								<reportElement x="0" y="0" width="94" height="60" uuid="8193ada3-65fc-4b87-bc83-053e43825db5"/>
								<textElement textAlignment="Center" verticalAlignment="Middle">
									<font fontName="宋体"/>
								</textElement>
								<text><![CDATA[数量或任务量]]></text>
							</staticText>
						</jr:columnHeader>
						<jr:detailCell style="Table_TD" height="30">
							<textField>
								<reportElement x="0" y="0" width="94" height="30" uuid="b41577f2-7478-4029-91db-36e15a3c7623"/>
								<textElement textAlignment="Right" verticalAlignment="Middle">
									<font fontName="宋体"/>
								</textElement>
								<textFieldExpression><![CDATA[$F{field3}]]></textFieldExpression>
							</textField>
						</jr:detailCell>
					</jr:column>
					<jr:columnGroup width="146" uuid="fdf3d400-8fe4-40bc-9e36-3c2fcd7a18f8">
						<property name="com.jaspersoft.studio.components.table.model.column.name" value="Columns [2]"/>
						<jr:columnHeader style="Table_CH" height="30" rowSpan="1">
							<staticText>
								<reportElement x="0" y="0" width="146" height="30" uuid="77a8469f-94a6-4fa7-ae06-ad32f5dde1fb"/>
								<textElement textAlignment="Center" verticalAlignment="Middle">
									<font fontName="宋体"/>
								</textElement>
								<text><![CDATA[价款或酬金]]></text>
							</staticText>
						</jr:columnHeader>
						<jr:column width="73" uuid="a3c03042-b6ba-4d24-b145-c4fb3cdaf5f4">
							<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="73" height="30" uuid="b8aa3420-60fc-4943-a5d5-4c889a6bca2c"/>
									<textElement textAlignment="Center" verticalAlignment="Middle">
										<font fontName="宋体"/>
									</textElement>
									<text><![CDATA[单价]]></text>
								</staticText>
							</jr:columnHeader>
							<jr:detailCell style="Table_TD" height="30">
								<textField>
									<reportElement x="0" y="0" width="73" height="30" uuid="12029967-c6af-4f16-aca5-c64c072e8184"/>
									<textElement textAlignment="Right" verticalAlignment="Middle">
										<font fontName="宋体"/>
									</textElement>
									<textFieldExpression><![CDATA[$F{field4}]]></textFieldExpression>
								</textField>
							</jr:detailCell>
						</jr:column>
						<jr:column width="73" uuid="54c1c748-f2c5-4747-ab92-38a282d76603">
							<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="73" height="30" uuid="afa17a51-eb97-4b55-9bda-9adf75b907a9"/>
									<textElement textAlignment="Center" verticalAlignment="Middle">
										<font fontName="宋体"/>
									</textElement>
									<text><![CDATA[总金额]]></text>
								</staticText>
							</jr:columnHeader>
							<jr:detailCell style="Table_TD" height="30">
								<textField>
									<reportElement x="0" y="0" width="73" height="30" uuid="021a7ec7-ed4a-48be-841b-e3d5e6422db2"/>
									<textElement textAlignment="Right" verticalAlignment="Middle">
										<font fontName="宋体"/>
									</textElement>
									<textFieldExpression><![CDATA[$F{field5}]]></textFieldExpression>
								</textField>
							</jr:detailCell>
						</jr:column>
					</jr:columnGroup>
				</jr:table>
			</componentElement>
		</band>
		<band height="270">
			<componentElement>
				<reportElement x="0" y="32" width="555" height="60" uuid="68b08e3a-fe5f-4069-a510-9511ab08120b">
					<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
					<property name="com.jaspersoft.studio.table.style.table_header" value="Table 1_TH"/>
					<property name="com.jaspersoft.studio.table.style.column_header" value="Table 1_CH"/>
					<property name="com.jaspersoft.studio.table.style.detail" value="Table 1_TD"/>
					<property name="com.jaspersoft.studio.unit.width" value="px"/>
					<property name="com.jaspersoft.studio.components.autoresize.proportional" value="true"/>
					<property name="com.jaspersoft.studio.unit.x" value="px"/>
				</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" whenNoDataType="AllSectionsNoDetail">
					<datasetRun subDataset="Dataset1" uuid="260fc936-1136-41bc-89da-d1bab0649a00">
						<dataSourceExpression><![CDATA[$F{tableData2}]]></dataSourceExpression>
					</datasetRun>
					<jr:column width="111" uuid="f38c5305-dbfc-4971-b9c0-66f462238cda">
						<jr:columnHeader style="Table 1_CH" height="30">
							<staticText>
								<reportElement x="0" y="0" width="111" height="30" uuid="31842e37-86ae-4e48-b1d5-6ad42e256668"/>
								<textElement verticalAlignment="Middle">
									<font fontName="宋体"/>
								</textElement>
								<text><![CDATA[材料名称]]></text>
							</staticText>
						</jr:columnHeader>
						<jr:detailCell style="Table 1_TD" height="30">
							<textField>
								<reportElement x="0" y="0" width="111" height="30" uuid="d7f388c4-d759-47d4-8892-49660e0ddf19"/>
								<textElement verticalAlignment="Middle">
									<font fontName="宋体"/>
								</textElement>
								<textFieldExpression><![CDATA[$F{field1}]]></textFieldExpression>
							</textField>
						</jr:detailCell>
					</jr:column>
					<jr:column width="111" uuid="033d0210-0f64-44b9-a649-e37469848229">
						<jr:columnHeader style="Table 1_CH" height="30">
							<staticText>
								<reportElement x="0" y="0" width="111" height="30" uuid="a5964748-a743-4841-b944-5c8835bfa13d"/>
								<textElement verticalAlignment="Middle">
									<font fontName="宋体"/>
								</textElement>
								<text><![CDATA[商标]]></text>
							</staticText>
						</jr:columnHeader>
						<jr:detailCell style="Table 1_TD" height="30">
							<textField>
								<reportElement x="0" y="0" width="111" height="30" uuid="3fc760d3-158c-4c10-9f1c-ced7139ea541"/>
								<textElement verticalAlignment="Middle">
									<font fontName="宋体"/>
								</textElement>
								<textFieldExpression><![CDATA[$F{field2}]]></textFieldExpression>
							</textField>
						</jr:detailCell>
					</jr:column>
					<jr:column width="111" uuid="50b10ddf-1739-4072-bafc-c24b1e54895f">
						<jr:columnHeader style="Table 1_CH" height="30">
							<staticText>
								<reportElement x="0" y="0" width="111" height="30" uuid="b0a1e517-3d17-4eeb-9f95-f797dfc67b85"/>
								<textElement verticalAlignment="Middle">
									<font fontName="宋体"/>
								</textElement>
								<text><![CDATA[生产厂家]]></text>
							</staticText>
						</jr:columnHeader>
						<jr:detailCell style="Table 1_TD" height="30">
							<textField>
								<reportElement x="0" y="0" width="111" height="30" uuid="6e9bca9e-0883-4f2b-b05c-62291ad127cb"/>
								<textElement verticalAlignment="Middle">
									<font fontName="宋体"/>
								</textElement>
								<textFieldExpression><![CDATA[$F{field3}]]></textFieldExpression>
							</textField>
						</jr:detailCell>
					</jr:column>
					<jr:column width="111" uuid="fd4c786d-5d63-4c98-aada-c63a0447c0bd">
						<jr:columnHeader style="Table 1_CH" height="30">
							<staticText>
								<reportElement x="0" y="0" width="111" height="30" uuid="34054435-c587-4ca1-b730-f8f956420fd7"/>
								<textElement verticalAlignment="Middle">
									<font fontName="宋体"/>
								</textElement>
								<text><![CDATA[规格型号]]></text>
							</staticText>
						</jr:columnHeader>
						<jr:detailCell style="Table 1_TD" height="30">
							<textField>
								<reportElement x="0" y="0" width="111" height="30" uuid="1251001d-0ad4-4f18-abec-aa0c0d1908c3"/>
								<textElement verticalAlignment="Middle">
									<font fontName="宋体"/>
								</textElement>
								<textFieldExpression><![CDATA[$F{field4}]]></textFieldExpression>
							</textField>
						</jr:detailCell>
					</jr:column>
					<jr:column width="111" uuid="4b895ac4-53a3-4d01-83b7-089f0c5194f7">
						<jr:columnHeader style="Table 1_CH" height="30">
							<staticText>
								<reportElement x="0" y="0" width="111" height="30" uuid="f6915d74-fdec-4175-9a35-a6870899ebd4"/>
								<textElement verticalAlignment="Middle">
									<font fontName="宋体"/>
								</textElement>
								<text><![CDATA[计量单位]]></text>
							</staticText>
						</jr:columnHeader>
						<jr:detailCell style="Table 1_TD" height="30">
							<textField>
								<reportElement x="0" y="0" width="111" height="30" uuid="759e2918-98a5-46a4-b6d3-66eb5a27621a"/>
								<textElement verticalAlignment="Middle">
									<font fontName="宋体"/>
								</textElement>
								<textFieldExpression><![CDATA[$F{field5}]]></textFieldExpression>
							</textField>
						</jr:detailCell>
					</jr:column>
				</jr:table>
			</componentElement>
			<textField evaluationTime="Band">
				<reportElement x="0" y="-1" width="555" height="30" uuid="165b5438-e528-44ff-8978-0b7c6593444e">
					<property name="com.jaspersoft.studio.unit.width" value="px"/>
					<property name="com.jaspersoft.studio.unit.y" value="px"/>
				</reportElement>
				<box>
					<leftPen lineWidth="0.5"/>
					<bottomPen lineWidth="0.5"/>
					<rightPen lineWidth="0.5"/>
				</box>
				<textElement verticalAlignment="Middle">
					<font fontName="宋体"/>
				</textElement>
				<textFieldExpression><![CDATA["合计人民币金额 (大写):"+$F{totalFee}]]></textFieldExpression>
			</textField>
			<image>
				<reportElement positionType="FixRelativeToBottom" x="420" y="120" width="130" height="130" uuid="3abecb68-6168-4280-ad4f-a5ac7fa48fe0">
					<property name="com.jaspersoft.studio.unit.width" value="px"/>
				</reportElement>
				<imageExpression><![CDATA[$F{imageUrl}]]></imageExpression>
			</image>
		</band>
	</detail>
	<lastPageFooter>
		<band height="21"/>
	</lastPageFooter>
</jasperReport>

2、Java的调用

      直接使用java的main方法测试,代码如下:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.jasperreports.engine.JRStyle;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.base.JRBaseStyle;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.export.ExporterInput;
import net.sf.jasperreports.export.OutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.view.JasperViewer;

public class RptTest {
	public static void main(String[] args) {
		JasperDesign design;
		try {
			design = JRXmlLoader.load("C:\\template\\test.jrxml");
			HashMap<String, Object> paramsMap = new HashMap<String, Object>();

			// 加载模板
			JasperReport report = JasperCompileManager.compileReport(design);

			List<RptData> detailList = new ArrayList<RptData>();
			RptData mainData = new RptData();
			mainData.setMakeUser("admin");
			mainData.setDocNo("2019文字-00001");
			mainData.setTotalFee("伍佰圆整");
			mainData.setImageUrl("C:\\images\\coffee.jpg");
			detailList.add(mainData);

			List<Fields> list1 = new ArrayList<Fields>();
			for (int i = 1; i <= 5; i++) {
				Fields data = new Fields();
				data.setField1("项目" + i);
				data.setField2("台");
				data.setField3("" + i);
				data.setField4("100.00");
				data.setField5(String.valueOf(i * 100));
				list1.add(data);
			}
			mainData.setTableData(list1);

			List<Fields> list2 = new ArrayList<Fields>();
			for (int i = 1; i <= 5; i++) {
				Fields data = new Fields();
				data.setField1("材料" + i);
				data.setField2("商标" + i);
				data.setField3("生产厂家" + i);
				data.setField4("规格型号");
				data.setField5("个");
				list2.add(data);
			}
			mainData.setTableData2(new JRBeanCollectionDataSource(list2));

			JRBeanCollectionDataSource jbds = new JRBeanCollectionDataSource(detailList);
			// 填充数据
			JasperPrint print = JasperFillManager.fillReport(report, paramsMap, jbds);

//				// 预览显示
//			JasperViewer.viewReport(print);

			JRPdfExporter exporter = new JRPdfExporter(); // 指定要导出的jrprit数据
			// 指定导出文件的文件名
			ExporterInput exporterInput = new SimpleExporterInput(print);
			exporter.setExporterInput(exporterInput);

			// 指定导出文件的文件名
			OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput("C:\\pdf\\test.pdf");
			exporter.setExporterOutput(exporterOutput);

			// 实现报表的导出
			exporter.exportReport();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 3、运行效果如下:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值