先看看设计的报表样式:
reportTitle是新添加的一个参数,而其他的id和name是通过数据源得到的Filed。IReport在设置参数的时候需要先在左边新建一个parameter,然后再把该parameter托到右边的设计栏中。
生成的XML文件为:
<?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="report3" language="groovy" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"> <property name="ireport.zoom" value="1.0"/> <property name="ireport.x" value="0"/> <property name="ireport.y" value="0"/> <queryString> <![CDATA[select * from t_blog;]]> </queryString> <field name="id" class="java.lang.Integer"/> <field name="content" class="java.lang.String"/> <field name="postTime" class="java.sql.Timestamp"/> <field name="seenCount" class="java.lang.Integer"/> <field name="title" class="java.lang.String"/> <field name="blogStore" class="java.lang.Integer"/> <field name="owner" class="java.lang.Integer"/> <field name="sysCategory" class="java.lang.Integer"/> <background> <band splitType="Stretch"/> </background> <title> <band height="44" splitType="Stretch"> <staticText> <reportElement x="227" y="12" width="100" height="20" forecolor="#FF0000"/> <textElement textAlignment="Center"> <font size="13" isBold="true"/> </textElement> <text><![CDATA[标题]]></text> </staticText> </band> </title> <pageHeader> <band height="35" splitType="Stretch"> <staticText> <reportElement x="227" y="15" width="100" height="20" forecolor="#FF0000"/> <textElement textAlignment="Center"> <font size="13" isBold="true"/> </textElement> <text><![CDATA[页眉]]></text> </staticText> </band> </pageHeader> <columnHeader> <band height="40" splitType="Stretch"> <staticText> <reportElement x="70" y="2" width="100" height="20"/> <textElement/> <text><![CDATA[id]]></text> </staticText> <staticText> <reportElement x="214" y="2" width="100" height="20"/> <textElement/> <text><![CDATA[title]]></text> </staticText> <staticText> <reportElement x="373" y="2" width="100" height="20"/> <textElement/> <text><![CDATA[postTime]]></text> </staticText> </band> </columnHeader> <detail> <band height="81" splitType="Stretch"> <textField> <reportElement x="70" y="24" width="100" height="20"/> <textElement/> <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression> </textField> <textField> <reportElement x="214" y="25" width="100" height="20"/> <textElement/> <textFieldExpression><![CDATA[$F{title}]]></textFieldExpression> </textField> <textField> <reportElement x="373" y="26" width="100" height="20"/> <textElement/> <textFieldExpression><![CDATA[$F{postTime}]]></textFieldExpression> </textField> </band> </detail> <columnFooter> <band height="40" splitType="Stretch"> <staticText> <reportElement x="227" y="10" width="100" height="20" forecolor="#FF0000"/> <textElement textAlignment="Center"> <font size="13" isBold="true"/> </textElement> <text><![CDATA[相当于表尾]]></text> </staticText> </band> </columnFooter> <pageFooter> <band height="43" splitType="Stretch"> <staticText> <reportElement x="227" y="13" width="100" height="20" forecolor="#FF0000"/> <textElement textAlignment="Center"> <font size="13" isBold="true"/> </textElement> <text><![CDATA[页脚]]></text> </staticText> </band> </pageFooter> <summary> <band height="41" splitType="Stretch"> <staticText> <reportElement x="227" y="10" width="100" height="20" forecolor="#FF0000"/> <textElement textAlignment="Center"> <font size="13" isBold="true"/> </textElement> <text><![CDATA[用于存放一些统计信息的]]></text> </staticText> </band> </summary> </jasperReport>
JasperReport在生成报表的时候可以直接操作jrxml文件,然后通过Java代码编译为.jasper文件,也可以直接操作IReport编译好的.jasper文件,下面就是用的直接操作.jasper文件,注释掉的是操作jrxml文件的。JasperReport可以把生成的报表导出为多种格式,下面导出的是普通的HTML。JasperReport在往模版里面设置参数值,是提供一个Map进行参数传入的。
java代码:
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; public class Test2 { public static void main(String args[]) throws JRException, ClassNotFoundException, SQLException, FileNotFoundException { String fileName = "E:\\reports\\report2.jrxml";//直接操作生成的jrxml文件 JasperReport jasperReport = null; JasperPrint jasperPrint = null; // jasperReport = JasperCompileManager.compileReport(fileName);//编译jrxml文件 InputStream inputStream = new FileInputStream("E:\\reports\\report2.jasper"); Map<String, Object> parameters = new HashMap<String, Object>();//传入参数 parameters.put("reportTitle", "我的第一个程序"); // jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, getConnection()); jasperPrint = JasperFillManager.fillReport(inputStream, parameters, getConnection()); JasperExportManager.exportReportToHtmlFile(jasperPrint, "first.html"); } public static Connection getConnection() throws ClassNotFoundException, SQLException { Connection conn = null; Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost/blog", "root", "root"); return conn; } }
导出的HTML预览界面: