webwork+jasperReport实践
所需jar包
dist/jasperreports-X.jar
lib/commons-*.jar (all the commons - except maybe for commons-logging)
lib/itext-X.jar
lib/jdt-compiler.jar
xwork.xml中设置
- <action name="PaymentToPDF" class="fr.simatai.webwork.payment.PaymentToPDF">
- <result name="success" type="jasper">
- <param name="location">/admin/payment/PaymentToPDF.jasper</param>
- <param name="dataSource">datalist</param>
- <param name="format">PDF</param>
- </result>
- </action>
类的编写PaymentToPDF.java
- public class PaymentToPDF extends ActionSupport {
- Log log = LogFactory.getLog(PaymentToPDF.class);
- List datalist = new ArrayList();
- String name;
- Float taxRate;
- getter..
- public String execute() throws Exception {
- List paymentList = new ArrayList();
- //这里是对数据获取的代码,一般是访问持久层
- return SUCCESS;
- }
- //编译与调试,如果用iReport,我发现并能不直接拿出来用 main函数中编译
- public static void main(String[] args) {
- try {
- JasperCompileManager.compileReportToFile(
- "WebRoot//admin//payment//PaymentToPDF.jrxml",
- "WebRoot//admin//payment//PaymentToPDF.jasper");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
PaymentToPDF.jrxml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN"
- "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
- <jasperReport name="sample_report" pageWidth="595" pageHeight="842" columnWidth="515" columnSpacing="0" leftMargin="40" rightMargin="40" topMargin="50" bottomMargin="50">
- 定义字体
- <reportFont name="Arial_Normal" isDefault="true" fontName="Arial" size="12" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
- <reportFont name="Arial_Small" isDefault="true" fontName="Arial" size="8" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
- <reportFont name="Arial_Bold" isDefault="false" fontName="Arial" size="12" isBold="true" pdfFontName="Helvetica-Bold" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
- <reportFont name="Arial_Italic" isDefault="false" fontName="Arial" size="12" isItalic="true" pdfFontName="Helvetica-Oblique" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
- 定义参数
- <parameter name="name" class="java.lang.String"/>类中用getter方法取得
- <parameter name="taxRate" class="java.lang.Float"/>类中用getter方法取得
- 计算值
- <variable name="sumtotal" class="java.lang.Float" resetType="Page" calculation="Sum">
- <variableExpression>$F{total}</variableExpression>
- <initialValueExpression>new Float(0)</initialValueExpression>
- </variable>
- <field name="balance" class="java.lang.Integer"/> datasource中定义一般是一个list对象,里面放的是entity bean
- <variable name="sumtotal" class="java.lang.Float" resetType="Page" calculation="Sum">
- <variableExpression>$F{total}</variableExpression>
- <initialValueExpression>new Float(0)</initialValueExpression>
- </variable>
- <title>
- <band height="0"/>
- </title>
- <pageHeader>
- <band height="155">
- 图片
- <image>
- <reportElement x="0" y="0" width="100" height="80"/>
- <imageExpression class="java.net.URL"><![CDATA[new URL("http://photo2:88/icon/file.gif")]]></imageExpression>
- </image>
- <image>
- <reportElement x="50" y="0" width="199" height="125"/>
- <imageExpression class="java.lang.String"><![CDATA["F://data//wwwroot//simatai//icon//pdf.jpg"]]></imageExpression>
- </image>
- <rectangle>
- <reportElement x="250" y="45" width="200" height="80"/>
- <graphicElement stretchType="NoStretch" pen="1Point"/>
- </rectangle>
- <textField>
- <reportElement x="250" y="45" width="200" height="15"/>
- <textElement textAlignment="Left" >
- <font reportFont="Arial_Bold" size="10"/>
- </textElement>
- <textFieldExpression class="java.lang.String"><![CDATA[" Adressée à :"]]></textFieldExpression>
- </textField>
- 这儿放不重复的字段...
- </band>
- </pageHeader>
- <columnHeader>
- <band height="160">
- <textField>
- <reportElement x="0" y="0" width="515" height="25"/>
- <textElement textAlignment="Left"/>
- <textFieldExpression class="java.lang.String">
- "DATE ET HEURE DE LA TRANSACTION : LE "+$P{todayStr}
- </textFieldExpression>
- </textField>
- 这儿放不重复的字段及表格标题...
- 定义静态文本
- <staticText>
- <reportElement x="445" y="100" width="70" height="60" />
- <textElement textAlignment="Center" verticalAlignment="Middle">
- <font reportFont="Arial_Bold" size="8"/>
- </textElement>
- <text><![CDATA[Montant en Euros]]></text>
- </staticText>
- </band>
- </columnHeader>
- <detail>
- <band height="16">
- 画框
- <rectangle>
- <reportElement x="0" y="5" width="515" height="15" backcolor="#666666"/>
- <graphicElement stretchType="NoStretch" pen="2Point"/>
- </rectangle>
- 动态文本
- <textField>
- <reportElement x="0" y="0" width="165" height="15" />
- <textElement>
- <font reportFont="Arial_Small"/>
- </textElement>
- <textFieldExpression><![CDATA[" "+$F{description}]]></textFieldExpression>
- </textField>
- 这儿放重复的字段及表格
- </band>
- </detail>
- <columnFooter>
- <band height="81">
- 画线
- <line>
- <reportElement x="115" y="80" width="300" height="0"/>
- <graphicElement stretchType="NoStretch"/>
- </line>
- </band>
- </columnFooter>
- <pageFooter>
- <band height="0"/>
- </pageFooter>
- </jasperReport>