1、Jasperreport官网下载地址:http://community.jaspersoft.com/download
下载jasperreports-5.5.1-project.zip和iReport-5.5.0-windows-installer.exe即可。
2、ireport的使用请参考网上其它文章,推荐http://blog.csdn.net/q326527970/article/details/7049047和图书《JAVA报表开发技术深入解析》
注意:使用ireport添加chart组件时,一定不要添加到Detail区,否则会出现多个重复图片的问题
3、生成pdf报表代码:
package com.csljc.jasper; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.HashMap; 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; import net.sf.jasperreports.engine.design.JasperDesign; import net.sf.jasperreports.engine.xml.JRXmlLoader; public class JasperMain { public static final String driver = "com.mysql.jdbc.Driver"; public static final String url = "jdbc:mysql://192.168.53.196:3306/zabbix"; public static final String user = "root"; public static final String password = ""; public static Connection getConnection(){ Connection conn = null; try { Class.forName(driver); conn = DriverManager.getConnection(url,user,password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } /** * @param args */ public static void main(String[] args) { Connection conn = null; try { //加载jasperreport模板文件 JasperDesign jd = JRXmlLoader.load("report1.jrxml"); //编译模板文件为JasperReport对象 JasperReport jr2 = JasperCompileManager.compileReport(jd); //存放需要传给JasperReport的参数 HashMap<String,Object> parameter = new HashMap<String,Object>(); //获取数据连接 conn = getConnection(); //将数据和参数填充到JasperReport中 JasperPrint jp = JasperFillManager.fillReport(jr2, parameter, conn); //导出为pdf文件 JasperExportManager.exportReportToPdfFile(jp, "D:\\test.pdf"); } catch (JRException e) { e.printStackTrace(); }finally{ if(conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }4、关于导出pdf报表不显示中文的问题
1)把iTextAsian.jar亚洲语言包放到项目中
2)在用iReport做模板设计,在设计pdf报表模板时注意,把所有涉及到的中文显示设置如下:
<textElement>
<font size="14" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/>
</textElement>
当前版本中pdfFontName属性已经废弃,但依旧可以使用,目前还不明白所谓的建议改为font extention如何设置