如何在JasperReports中使用XML文件作为数据源?
来源:互联网 发布:淘宝美购国际是正品吗 编辑:程序博客网 时间:2024/04/30 12:55
1.报表设计
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="OrdersReport" pageWidth="500" pageHeight="842" columnWidth="500" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
<style name="Arial_Normal" isDefault="true" fontName="Arial" fontSize="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<style name="Arial_Bold" isDefault="false" fontName="Arial" fontSize="8" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica-Bold" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<style name="Arial_Italic" isDefault="false" fontName="Arial" fontSize="8" isBold="false" isItalic="true" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica-Oblique" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<parameter name="CustomerID" class="java.lang.String"/>
<queryString language="xPath"><![CDATA[/Northwind/Orders[CustomerID='$P{CustomerID}']]]></queryString>
<field name="Id" class="java.lang.String">
<fieldDescription><![CDATA[OrderID]]></fieldDescription>
</field>
<field name="OrderDate" class="java.lang.String">
<fieldDescription><![CDATA[OrderDate]]></fieldDescription>
</field>
<field name="ShipCity" class="java.lang.String">
<fieldDescription><![CDATA[ShipCity]]></fieldDescription>
</field>
<field name="Freight" class="java.lang.Float">
<fieldDescription><![CDATA[Freight]]></fieldDescription>
</field>
<variable name="TotalFreight" class="java.lang.Float" calculation="Sum">
<variableExpression><![CDATA[$F{Freight}]]></variableExpression>
</variable>
<pageHeader>
<band height="14">
<rectangle>
<reportElement x="0" y="2" width="356" height="10" forecolor="#ccffff" backcolor="#ccffff"/>
<graphicElement/>
</rectangle>
<staticText>
<reportElement mode="Opaque" x="0" y="2" width="48" height="10" backcolor="#ccffff" style="Arial_Bold"/>
<textElement textAlignment="Right"/>
<text><![CDATA[ID]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="54" y="2" width="87" height="10" backcolor="#ccffff" style="Arial_Bold"/>
<textElement textAlignment="Center"/>
<text><![CDATA[Order Date]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="146" y="2" width="108" height="10" backcolor="#ccffff" style="Arial_Bold"/>
<text><![CDATA[Ship City]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="259" y="2" width="92" height="10" backcolor="#ccffff" style="Arial_Bold"/>
<textElement textAlignment="Right"/>
<text><![CDATA[Freight]]></text>
</staticText>
</band>
</pageHeader>
<detail>
<band height="14">
<textField>
<reportElement x="0" y="2" width="51" height="10"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{Id}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" x="54" y="2" width="87" height="10"/>
<textElement textAlignment="Center"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{OrderDate}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" x="146" y="2" width="108" height="10"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{ShipCity}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" x="259" y="2" width="92" height="10"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.Float"><![CDATA[$F{Freight}]]></textFieldExpression>
</textField>
</band>
</detail>
<summary>
<band height="14">
<rectangle>
<reportElement x="0" y="2" width="356" height="10" forecolor="#33cccc" backcolor="#33cccc"/>
<graphicElement pen="Thin"/>
</rectangle>
<staticText>
<reportElement mode="Opaque" x="160" y="2" width="67" height="10" backcolor="#33cccc" style="Arial_Bold"/>
<textElement textAlignment="Right"/>
<text><![CDATA[Total :]]></text>
</staticText>
<textField>
<reportElement mode="Opaque" x="259" y="2" width="92" height="10" backcolor="#33cccc" style="Arial_Bold"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.Float"><![CDATA[$V{TotalFreight}]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="227" y="2" width="27" height="10" backcolor="#33cccc" style="Arial_Bold"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.Integer"><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
</textField>
</band>
</summary>
</jasperReport>
2.报表填充
...
Document document = JRXmlUtils.parse(new File("northwind.xml"));
params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, document);
JasperFillManager.fillReportToFile(fileName, params);
...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="OrdersReport" pageWidth="500" pageHeight="842" columnWidth="500" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
<style name="Arial_Normal" isDefault="true" fontName="Arial" fontSize="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<style name="Arial_Bold" isDefault="false" fontName="Arial" fontSize="8" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica-Bold" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<style name="Arial_Italic" isDefault="false" fontName="Arial" fontSize="8" isBold="false" isItalic="true" isUnderline="false" isStrikeThrough="false" pdfFontName="Helvetica-Oblique" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
<parameter name="CustomerID" class="java.lang.String"/>
<queryString language="xPath"><![CDATA[/Northwind/Orders[CustomerID='$P{CustomerID}']]]></queryString>
<field name="Id" class="java.lang.String">
<fieldDescription><![CDATA[OrderID]]></fieldDescription>
</field>
<field name="OrderDate" class="java.lang.String">
<fieldDescription><![CDATA[OrderDate]]></fieldDescription>
</field>
<field name="ShipCity" class="java.lang.String">
<fieldDescription><![CDATA[ShipCity]]></fieldDescription>
</field>
<field name="Freight" class="java.lang.Float">
<fieldDescription><![CDATA[Freight]]></fieldDescription>
</field>
<variable name="TotalFreight" class="java.lang.Float" calculation="Sum">
<variableExpression><![CDATA[$F{Freight}]]></variableExpression>
</variable>
<pageHeader>
<band height="14">
<rectangle>
<reportElement x="0" y="2" width="356" height="10" forecolor="#ccffff" backcolor="#ccffff"/>
<graphicElement/>
</rectangle>
<staticText>
<reportElement mode="Opaque" x="0" y="2" width="48" height="10" backcolor="#ccffff" style="Arial_Bold"/>
<textElement textAlignment="Right"/>
<text><![CDATA[ID]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="54" y="2" width="87" height="10" backcolor="#ccffff" style="Arial_Bold"/>
<textElement textAlignment="Center"/>
<text><![CDATA[Order Date]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="146" y="2" width="108" height="10" backcolor="#ccffff" style="Arial_Bold"/>
<text><![CDATA[Ship City]]></text>
</staticText>
<staticText>
<reportElement mode="Opaque" x="259" y="2" width="92" height="10" backcolor="#ccffff" style="Arial_Bold"/>
<textElement textAlignment="Right"/>
<text><![CDATA[Freight]]></text>
</staticText>
</band>
</pageHeader>
<detail>
<band height="14">
<textField>
<reportElement x="0" y="2" width="51" height="10"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{Id}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" x="54" y="2" width="87" height="10"/>
<textElement textAlignment="Center"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{OrderDate}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" x="146" y="2" width="108" height="10"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{ShipCity}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement positionType="Float" x="259" y="2" width="92" height="10"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.Float"><![CDATA[$F{Freight}]]></textFieldExpression>
</textField>
</band>
</detail>
<summary>
<band height="14">
<rectangle>
<reportElement x="0" y="2" width="356" height="10" forecolor="#33cccc" backcolor="#33cccc"/>
<graphicElement pen="Thin"/>
</rectangle>
<staticText>
<reportElement mode="Opaque" x="160" y="2" width="67" height="10" backcolor="#33cccc" style="Arial_Bold"/>
<textElement textAlignment="Right"/>
<text><![CDATA[Total :]]></text>
</staticText>
<textField>
<reportElement mode="Opaque" x="259" y="2" width="92" height="10" backcolor="#33cccc" style="Arial_Bold"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.Float"><![CDATA[$V{TotalFreight}]]></textFieldExpression>
</textField>
<textField>
<reportElement mode="Opaque" x="227" y="2" width="27" height="10" backcolor="#33cccc" style="Arial_Bold"/>
<textElement textAlignment="Right"/>
<textFieldExpression class="java.lang.Integer"><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
</textField>
</band>
</summary>
</jasperReport>
2.报表填充
...
Document document = JRXmlUtils.parse(new File("northwind.xml"));
params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, document);
JasperFillManager.fillReportToFile(fileName, params);
...
- 如何在JasperReports中使用XML文件作为数据源?
- 如何在JasperReports中使用CSV文件作为数据源?
- .NET : 在单元测试中使用外部文件作为数据源
- .NET : 在单元测试中使用外部文件作为数据源
- WPF 中使用XML作为Binding数据源
- 在BIRT中使用esProc作为数据源
- 如何在JasperReports中使用hibernate的查询?
- .net中XML文件作为数据源的操作类
- FastReport.Net报表中使用 Excel 文件作为数据源
- 在报表工具中使用esProc作为数据源
- SSIS如何在包中使用数据源
- SSIS如何在包中使用数据源
- 在Spring的xml文件中配置数据源主要代码
- 在Spring的 applicationContext.xml文件中配置数据源
- Web设计中如何使用XML数据源对象
- Web设计中如何使用XML数据源对象
- Web设计中如何使用XML数据源对象(2)
- Web设计中如何使用XML数据源对象(1)
- 求职一月记(2)
- 感谢网络的学习平台
- 在Struts中,用向导生成Form, Action&Jsp,如果删除Form需要注意事项
- 表空间的创建
- 始终会用上的Common BeanUtils
- 如何在JasperReports中使用XML文件作为数据源?
- 《第五章 隐藏具体实现》总结
- 软件项目管理杂谈
- WINNT下隐藏木马的进程 DLL木马篇
- ACCESS数据库中TEXT字段的真实长度
- Linux下用Valgrind检测内存泄露
- 莫拉蒂和4个干儿子的情感
- C# udp 通信 例子
- asp.net 显示柱状图