如何用表格格式设计报表? [英] How to design report with tabular format?
问题描述
我要求在ireport设计。
I have a requirement to design in ireport.
我有三个VO
DeliveryAllocations {
private String collectorCode;
private String collectorName;
private String month;
private List<PlantVO> plants = new ArrayList<PlantVO>();
with setter/getters
}
PlantVO{
private String plantCode;
private String plantName;
private String TotalWeight;
private List<PlantAllocationVO> allocations = new ArrayList<PlantAllocationVO>();
with setter/getters
}
PlantAllocationVO{
private String weight;
private String customerType;
private String customerValue;
private String comment;
private String date;
with setters/getters
}
现在我想要在表格格式的报告中显示 PlantVO
中的两个字段和 PlantAllocationVO
中的三个字段。
Now I want to display two fields from PlantVO
and three fields from PlantAllocationVO
in a report with tabular format.
我如何实现这一目标?
如何获取数据以设计具有这种结构的报告?
How do I achieve this? How do I fetch the data to design a report with this kind of structure?
推荐答案
这可以通过使用子报告来完成并作为数据源传递
This is done by for example using a subreport and passing as datasource
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($ F {allocations})
示例代码:
主报告( PlantVO.jrxml
)
Main report (PlantVO.jrxml
)
<?xml version="1.0" encoding="UTF-8"?>
<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="PlantVO" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="55da4c97-0a7c-447f-b3a7-2713d483523d">
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["C:\\jdd\\projects\\StackTrace\\jasper\\"]]></defaultValueExpression>
</parameter>
<field name="plantName" class="java.lang.String"/>
<field name="totalWeight" class="java.lang.Double"/>
<field name="allocations" class="java.util.List"/>
<detail>
<band height="60" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="211879f7-331e-4526-bb0d-e7f0314f71b3"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA["Plant name: " + $V{REPORT_COUNT}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="200" height="20" uuid="f2d206d5-61fb-4238-93cf-c7dd16403a48"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{plantName}]]></textFieldExpression>
</textField>
<subreport>
<reportElement x="100" y="20" width="400" height="20" uuid="f3ca3eba-cb93-4ab4-8931-c399a8430841"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{allocations})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "PlantAllocationVO_subreport.jasper"]]></subreportExpression>
</subreport>
<staticText>
<reportElement positionType="Float" x="100" y="40" width="200" height="20" uuid="db7b40e6-37fe-4815-b912-fc5afaf966fb"/>
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<text><![CDATA[TOTAL WEIGHT:]]></text>
</staticText>
<textField pattern="###0">
<reportElement positionType="Float" x="300" y="40" width="200" height="20" uuid="f1670bb4-efec-492c-b69c-0de668bda244"/>
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{totalWeight}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
子报告( PlantAllocationVO_subreport
)
Sub report (PlantAllocationVO_subreport
)
<?xml version="1.0" encoding="UTF-8"?>
<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="PlantAllocationVO_subreport" pageWidth="400" pageHeight="802" columnWidth="400" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="417a7f13-6776-4773-94ab-0be5c01605c7">
<field name="date" class="java.lang.String"/>
<field name="weight" class="java.lang.Double"/>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="200" height="20" uuid="34424fa2-18d0-4859-825a-a07f2a826f55"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
</textField>
<textField pattern="###0">
<reportElement x="200" y="0" width="200" height="20" uuid="a0e2ae10-906e-4d0f-aebd-30fc0c694aca"/>
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{weight}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
使用主代码
JasperReport report = JasperCompileManager.compileReport("PlantVO.jrxml");
Map<String, Object> map = new HashMap<String, Object>();
JasperPrint print = JasperFillManager.fillReport(report, map,new JRBeanCollectionDataSource(deliveryAllocations.getPlants()));
JasperExportManager.exportReportToPdfFile(print, "PlantVO.pdf");
将产生结果:
一些设计说明:
-
我会保持正确的字段类型(例如
权重
为Double
或
整数
)并在jrxml中应用模式(参见示例),这将帮助
你正确导出到excel。
I would keep correct type of fields (es.
weight
as aDouble
orInteger
) and apply pattern in jrxml (see example), this will help you do correct export to excel.
TotalWeight
确实没有必要将此字段作为字段,可以通过子报表计算
并传回主报告。
TotalWeight
there is no really need to have this as a field, it could
be calculated by subreport and passed back to main report.
玩得开心
这篇关于如何用表格格式设计报表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!