注意:编写此项目时,请先手动编写**.jrxml
或者用IReport生成**.jrxml或**.jasper
注:java导出报表与web项目显示报表方法其实是相同的,只不过有一句代码不同,如下
- html.setParameter(JRHtmlExporterParameter.OUTPUT_FILE_NAME,"C:/Users/Administrator/Desktop/ireport/report1.html");//生成文件
- //html.setParameter(JRHtmlExporterParameter.OUTPUT_WRITER,response.getWriter()); // web页面显示
maven依赖
- <!-- iReport JasperReports -->
- <dependency>
- <groupId>net.sf.jasperreports</groupId>
- <artifactId>jasperreports</artifactId>
- <version>5.6.0</version>
- </dependency>
- <dependency>
- <groupId>org.codehaus.groovy</groupId>
- <artifactId>groovy-all</artifactId>
- <version>2.2.2</version>
- </dependency>
通过java代码生成各种格式报表文件
JDBC文件
- package com.hlzt.test;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
-
- public class JDBC {
- public static String driver="com.mysql.jdbc.Driver";
- public static String url="jdbc:mysql://localhost:3306/senyuan1";
- public static String user="root";
- public static String pwd="123456";
- public static Connection conn=getConnection();
- public static Statement statement=getStatement();
-
- private static Connection getConnection(){
- if(conn==null){
- try{
- Class.forName(driver);
- conn=DriverManager.getConnection(url,user,pwd);
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- return conn;
- }
- private static Statement getStatement(){
- if(statement==null){
- try{
- statement=conn.createStatement();
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- return statement;
- }
- public static ResultSet getResultSet(String sql){
- ResultSet rs=null;
- try{
- rs=statement.executeQuery(sql);
- }catch(Exception e){
- e.printStackTrace();
- }
- return rs;
- }
- public static void closeAll(ResultSet rs,Statement st,Connection cn){
- try{
- if(rs!=null){
- rs.close();
- }
- if(st!=null){
- st.close();
- }
- if(cn!=null){
- cn.close();
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
- package com.hlzt.test;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.HashMap;
- import java.util.Map;
-
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import net.sf.jasperreports.engine.JRExporterParameter;
- import net.sf.jasperreports.engine.JasperCompileManager;
- import net.sf.jasperreports.engine.JasperFillManager;
- import net.sf.jasperreports.engine.JasperPrint;
- import net.sf.jasperreports.engine.JasperReport;
- import net.sf.jasperreports.engine.export.JRHtmlExporter;
- import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
- import net.sf.jasperreports.engine.util.JRLoader;
- @SuppressWarnings("deprecation")
- public class TestReport {
-
- public static void main(String[] args) {
- service();
- }
- //protected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
- protected static void service(){
- try{
- //ServletContext context = this.getServletConfig().getServletContext();
-
- //JasperCompileManager 编译管理器
- //JasperFillManager 填充管理器
- //JRXmlLoader xml加载器
- //JasperPrintManager 打印管理器
- //JasperExportManager 导出管理器
-
- //JasperCompileManager.compileReportToFile(context.getRealPath("/reports/DbReport.jrxml"));//编译jrxml文件,生成jasper文件
-
- Map map=new HashMap();//参数map
- map.put("userName", "admin");
-
- //生成jrprint文件
- //JasperFillManager.fillReportToFile(context.getRealPath("/reports/DbReport.jasper"), map, JDBC.conn);
-
-
- File jasperFile=new File("C:/Users/Administrator/Desktop/ireport/report1.jasper");
- JasperReport jasperReport =(JasperReport)JRLoader.loadObject(jasperFile);
- JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map,JDBC.conn);
- File file=new File("C:/Users/Administrator/Desktop/ireport/report1.html");
- //生成html
- JRHtmlExporter html = new JRHtmlExporter();
- html.setParameter(JRHtmlExporterParameter.JASPER_PRINT, jasperPrint);
- html.setParameter(JRHtmlExporterParameter.OUTPUT_FILE_NAME,"C:/Users/Administrator/Desktop/ireport/report1.html");//生成文件
- //html.setParameter(JRHtmlExporterParameter.OUTPUT_WRITER,response.getWriter()); // web页面显示
- html.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE);
- html.setParameter(JRExporterParameter.CHARACTER_ENCODING, "utf-8");
- html.exportReport();
- //生成excel
- /*
- JRXlsExporter xls=new JRXlsExporter();
- */
- //生成pdf
- /*
- JRPdfExporter pdf = new JRPdfExporter();
- */
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
与javaweb整合
本文用的是JasperReport最新版本6.0
JasperReport官网:community.jaspersoft.com
web开发建议下载JasperReports library --> jasperreports-xxx-project 里面包含demo以及api
项目结构:
jar包:新手可以把下载下来的所有jar包都放进去,熟练之后可以根据自己所需加入相关jar包
WebRoot下:新建reports文件夹,用来存放jrxml、jasper文件;build.xml(放入进去之后会出现蚂蚁图标);
jrxml文件代码,此代码是xml格式文件,可以自己手写,也可以安装 iReport设计编译成jasper文件,为了入门下面纯手写
DbReport.jrxml:
后台代码:TestServlet.java
JDBC:
执行效果:html数据