1. 用ireport绘制出.jrxml文件
<?xml version="1.0" encoding="UTF-8" ?> <!-- Created with iReport - A designer for JasperReports --> <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"> <jasperReport name="hejie" columnCount="1" printOrder="Vertical" orientation="Portrait" pageWidth="595" pageHeight="842" columnWidth="535" columnSpacing="0" leftMargin="30" rightMargin="30" topMargin="20" bottomMargin="20" whenNoDataType="NoPages" isTitleNewPage="false" isSummaryNewPage="false"> <property name="ireport.scriptlethandling" value="2" /> <property name="ireport.encoding" value="UTF-8" /> <import value="java.util.*" /> <import value="net.sf.jasperreports.engine.*" /> <import value="net.sf.jasperreports.engine.data.*" /> <queryString><![CDATA[select * from testhe]]></queryString> <field name="id" class="java.lang.Integer"/> <field name="name" class="java.lang.String"/> <background> <band height="0" isSplitAllowed="true" > </band> </background> <title> <band height="50" isSplitAllowed="true" > <staticText> <reportElement x="200" y="11" width="124" height="35" key="staticText-1"/> <box></box> <textElement textAlignment="Center"> <font size="18"/> </textElement> <text><![CDATA[My report]]></text> </staticText> </band> </title> <pageHeader> <band height="0" isSplitAllowed="true" > </band> </pageHeader> <columnHeader> <band height="30" isSplitAllowed="true" > <staticText> <reportElement x="37" y="6" width="96" height="22" key="staticText-2"/> <box></box> <textElement textAlignment="Left"> <font/> </textElement> <text><![CDATA[ID]]></text> </staticText> <staticText> <reportElement x="346" y="4" width="117" height="25" key="staticText-3"/> <box></box> <textElement textAlignment="Left"> <font/> </textElement> <text><![CDATA[NAME]]></text> </staticText> </band> </columnHeader> <detail> <band height="30" isSplitAllowed="true" > <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" > <reportElement x="37" y="6" width="96" height="18" key="textField"/> <box></box> <textElement> <font/> </textElement> <textFieldExpression class="java.lang.Integer"><![CDATA[$F{id}]]></textFieldExpression> </textField> <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" > <reportElement x="346" y="6" width="117" height="18" key="textField"/> <box></box> <textElement> <font/> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[$F{name}]]></textFieldExpression> </textField> </band> </detail> <columnFooter> <band height="30" isSplitAllowed="true" > </band> </columnFooter> <pageFooter> <band height="50" isSplitAllowed="true" > </band> </pageFooter> <lastPageFooter> <band height="50" isSplitAllowed="true" > </band> </lastPageFooter> <summary> <band height="50" isSplitAllowed="true" > </band> </summary> </jasperReport>
2.实体类,也可以是vo
public class TestHe {
private Integer id;
private String name;
...
}
3.过滤器,用于过滤集合中要显示的数据
import java.util.List;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
public class MyJRDataSource implements JRDataSource {
private List datas; // 从数据库中查询出来的集合
private int loop = -1; //当前遍历点
public MyJRDataSource(List datas) { //初始化用
super();
this.datas = datas;
}
public MyJRDataSource() {
super();
// TODO Auto-generated constructor stub
}
@Override
public Object getFieldValue(JRField field) throws JRException { //过滤主要部分,他跟next交叉运行的
// TODO Auto-generated method stub
String fname = field.getName();
TestHe th = (TestHe) this.datas.get(loop);
if("id".equals(fname)){
return th.getId();
}
if("name".equals(fname)){
return th.getName();
}
return null;
}
@Override
public boolean next() throws JRException {//移动loop
// TODO Auto-generated method stub
loop++;
if (loop >= datas.size()) {
return false;
} else {
return true;
}
}
}
4. 主类,东西比较多烦了点
import java.io.File;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
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 Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// getJrxml();
try {
JasperReport jr = getJR();//.....
System.out.println(jr);
List l = getList();//......
getReport(jr,l); //....
System.out.println(l.size());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//废弃
public static void getJrxml(){
InputStream is = Test.class.getClassLoader().getResourceAsStream("test.jrxml");
System.out.println(is);
}
//从数据库中查询查数据
public static List getList() throws ClassNotFoundException, SQLException{
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/hejietest";
String username = "root";
String password = "hejie";
Class.forName(driver);
List<TestHe> list = null;
Statement sta = null;
ResultSet rs = null;
Connection con = null;
try {
con = DriverManager.getConnection(url,username,password);
sta = con.createStatement();
rs = sta.executeQuery("select id ,name from testhe");
list = new ArrayList<TestHe> ();
while(rs.next()){
TestHe th = new TestHe();
th.setId(rs.getInt(1));
th.setName(rs.getString(2));
list.add(th);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(rs!=null)rs.close();
if(sta!=null)sta.close();
if(con!=null)con.close();
}
return list;
}
//拿到对于德jasperreport,jasperreport就是我们要建立的报表对象
public static JasperReport getJR() throws JRException{
String dir = "";
dir = new File("test.jrxml").getAbsolutePath();
System.out.println(dir);
return JasperCompileManager.compileReport(dir);
}//导出报表
public static void getReport(JasperReport jr , List<TestHe> ths) throws JRException{
JasperPrint jasperPrint = JasperFillManager.fillReport(jr,null,new MyJRDataSource(ths));
JasperExportManager.exportReportToPdfFile(jasperPrint, "test.pdf");
// System.out.println(ex);
System.out.println("ok");
}
}