`
happy100583
  • 浏览: 122576 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

JasperReport实例

阅读更多
Java代码 复制代码
  1. package test.report;   
  2.   
  3. import java.math.BigDecimal;   
  4. import java.sql.Connection;   
  5. import java.sql.DriverManager;   
  6. import java.sql.PreparedStatement;   
  7. import java.sql.ResultSet;   
  8. import java.sql.SQLException;   
  9. import java.util.HashMap;   
  10.   
  11. import net.sf.jasperreports.engine.JRException;   
  12. import net.sf.jasperreports.engine.JRResultSetDataSource;   
  13. import net.sf.jasperreports.engine.JasperCompileManager;   
  14. import net.sf.jasperreports.engine.JasperExportManager;   
  15. import net.sf.jasperreports.engine.JasperFillManager;   
  16. import net.sf.jasperreports.engine.JasperPrint;   
  17. import net.sf.jasperreports.engine.JasperReport;   
  18. import net.sf.jasperreports.engine.util.JRLoader;   
  19.   
  20. public class GenerateReport {   
  21.    public static void main(String[] args) {   
  22.       try {   
  23.          sql4Report();   
  24.       } catch (JRException e) {   
  25.          e.printStackTrace();   
  26.       } catch (SQLException e) {   
  27.          e.printStackTrace();   
  28.       } finally {   
  29.          System.out.print( "finish" );   
  30.       }   
  31.    }   
  32.        
  33.    public static void parameter4Report ()    
  34.       throws SQLException, JRException {   
  35.       //report parameter   
  36.       HashMap<String, Object> parameter =    
  37.          new HashMap<String, Object>();   
  38.       parameter.put( "master_location_id"new BigDecimal(22) );   
  39.   
  40.       Connection con = getConnection();   
  41.       JasperReport jasperReport =    
  42.          JasperCompileManager.compileReport("reports/Location.jrxml");   
  43.       JasperPrint jasperPrint =    
  44.          JasperFillManager.fillReport( jasperReport, parameter, con );   
  45.            
  46.       JasperExportManager.exportReportToPdfFile(   
  47.          jasperPrint, "reports/location_parameter4Report.pdf");        
  48.    }   
  49.        
  50.    public static void sql4Report () throws SQLException, JRException {   
  51.       //create the ResultSet   
  52.       Connection con = getConnection();   
  53.       PreparedStatement statement =    
  54.          con.prepareStatement(    
  55.             "select * from loc_location "  
  56.           + "where location_type_cd = 'ROOM' "  
  57.           + "order by location_type_cd"    
  58.          );   
  59.       ResultSet resultSet = statement.executeQuery();   
  60.            
  61.       JRResultSetDataSource result =    
  62.          new JRResultSetDataSource( resultSet );   
  63.       JasperReport jasperReport =    
  64.          JasperCompileManager.compileReport("reports/Location.jrxml");   
  65.       JasperPrint jasperPrint =    
  66.          JasperFillManager.fillReport(    
  67.             jasperReportnew HashMap(), result    
  68.          );   
  69.            
  70.       JasperExportManager.exportReportToPdfFile(   
  71.          jasperPrint, "reports/location_sql4Report.pdf"  
  72.       );   
  73.    }   
  74.        
  75.    public static void jasper4Report ()    
  76.       throws SQLException, JRException {   
  77.       //create the ResultSet   
  78.       Connection con = getConnection();   
  79.       PreparedStatement statement =    
  80.          con.prepareStatement(    
  81.             "select * from loc_location "  
  82.           + "where location_type_cd = 'ROOM' "  
  83.           + "order by location_type_cd"    
  84.          );   
  85.       ResultSet resultSet = statement.executeQuery();   
  86.            
  87.       JRResultSetDataSource result =    
  88.          new JRResultSetDataSource( resultSet );   
  89.       //modify   
  90.       JasperReport jasperReport =    
  91.          (JasperReport) JRLoader.loadObject(   
  92.             "reports/Location.jasper"  
  93.          );   
  94.       JasperPrint jasperPrint =    
  95.          JasperFillManager.fillReport(    
  96.             jasperReportnew HashMap(), result   
  97.          );   
  98.            
  99.       JasperExportManager.exportReportToPdfFile(   
  100.          jasperPrint, "reports/location_jasper4Report.pdf"  
  101.       );   
  102.    }   
  103.   
  104.    public static void xls4Report () throws SQLException, JRException {   
  105. //    create the ResultSet   
  106.       Connection con = getConnection();   
  107.       PreparedStatement statement =    
  108.          con.prepareStatement(   
  109.             "select * from loc_location "  
  110.           + "where location_type_cd = 'ROOM' "  
  111.           + "order by location_type_cd"    
  112.          );   
  113.       ResultSet resultSet = statement.executeQuery();   
  114.            
  115.            
  116.       JRResultSetDataSource result =    
  117.          new JRResultSetDataSource( resultSet );   
  118. //    create JasperReport from .jasper   
  119.       JasperReport jasperReport =    
  120.          (JasperReport) JRLoader.loadObject("reports/Location.jasper");   
  121.       JasperPrint jasperPrint =    
  122.          JasperFillManager.fillReport( jasperReportnew HashMap(), result );   
  123.            
  124. //        JasperExportManager.exportReportToPdfFile( jasperPrint, "reports/location_xls4Report.xls");   
  125.            
  126.       JRXlsExporter xlsExporter = new JRXlsExporter();   
  127.       xlsExporter.setParameter( JRExporterParameter.JASPER_PRINT, jasperPrint );   
  128.       xlsExporter.setParameter( JRExporterParameter.OUTPUT_FILE, new File("reports/location_xls4Report.xls") );   
  129.       xlsExporter.exportReport();   
  130.    }   
  131.        
  132.    private static Connection getConnection () throws SQLException {   
  133.       DriverManager.registerDriver(    
  134.          new oracle.jdbc.driver.OracleDriver()   
  135.       );     
  136.       return DriverManager.getConnection( url, user, pw );   
  137.    }   
  138.        
  139.    private static String url = "jdbc:oracle:thin:@localhost:1521:db";   
  140.    private static String user = "report";   
  141.    private static String pw   = "report";   
  142. }  
package test.report;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
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.util.JRLoader;

public class GenerateReport {
   public static void main(String[] args) {
      try {
         sql4Report();
      } catch (JRException e) {
         e.printStackTrace();
      } catch (SQLException e) {
         e.printStackTrace();
      } finally {
         System.out.print( "finish" );
      }
   }
	
   public static void parameter4Report () 
      throws SQLException, JRException {
      //report parameter
      HashMap<String, Object> parameter = 
         new HashMap<String, Object>();
      parameter.put( "master_location_id", new BigDecimal(22) );

      Connection con = getConnection();
      JasperReport jasperReport = 
         JasperCompileManager.compileReport("reports/Location.jrxml");
      JasperPrint jasperPrint = 
         JasperFillManager.fillReport( jasperReport, parameter, con );
		
      JasperExportManager.exportReportToPdfFile(
         jasperPrint, "reports/location_parameter4Report.pdf");	    
   }
	
   public static void sql4Report () throws SQLException, JRException {
      //create the ResultSet
      Connection con = getConnection();
      PreparedStatement statement = 
         con.prepareStatement( 
            "select * from loc_location "
          + "where location_type_cd = 'ROOM' "
          + "order by location_type_cd" 
         );
      ResultSet resultSet = statement.executeQuery();
		
      JRResultSetDataSource result = 
         new JRResultSetDataSource( resultSet );
      JasperReport jasperReport = 
         JasperCompileManager.compileReport("reports/Location.jrxml");
      JasperPrint jasperPrint = 
         JasperFillManager.fillReport( 
            jasperReport, new HashMap(), result 
         );
		
      JasperExportManager.exportReportToPdfFile(
         jasperPrint, "reports/location_sql4Report.pdf"
      );
   }
	
   public static void jasper4Report () 
      throws SQLException, JRException {
      //create the ResultSet
      Connection con = getConnection();
      PreparedStatement statement = 
         con.prepareStatement( 
            "select * from loc_location "
          + "where location_type_cd = 'ROOM' "
          + "order by location_type_cd" 
         );
      ResultSet resultSet = statement.executeQuery();
		
      JRResultSetDataSource result = 
         new JRResultSetDataSource( resultSet );
      //modify
      JasperReport jasperReport = 
         (JasperReport) JRLoader.loadObject(
            "reports/Location.jasper"
         );
      JasperPrint jasperPrint = 
         JasperFillManager.fillReport( 
            jasperReport, new HashMap(), result
         );
		
      JasperExportManager.exportReportToPdfFile(
         jasperPrint, "reports/location_jasper4Report.pdf"
      );
   }

   public static void xls4Report () throws SQLException, JRException {
//    create the ResultSet
      Connection con = getConnection();
      PreparedStatement statement = 
         con.prepareStatement(
            "select * from loc_location "
          + "where location_type_cd = 'ROOM' "
          + "order by location_type_cd" 
         );
      ResultSet resultSet = statement.executeQuery();
		
		
      JRResultSetDataSource result = 
         new JRResultSetDataSource( resultSet );
//    create JasperReport from .jasper
      JasperReport jasperReport = 
         (JasperReport) JRLoader.loadObject("reports/Location.jasper");
      JasperPrint jasperPrint = 
         JasperFillManager.fillReport( jasperReport, new HashMap(), result );
		
//        JasperExportManager.exportReportToPdfFile( jasperPrint, "reports/location_xls4Report.xls");
        
      JRXlsExporter xlsExporter = new JRXlsExporter();
      xlsExporter.setParameter( JRExporterParameter.JASPER_PRINT, jasperPrint );
      xlsExporter.setParameter( JRExporterParameter.OUTPUT_FILE, new File("reports/location_xls4Report.xls") );
      xlsExporter.exportReport();
   }
	
   private static Connection getConnection () throws SQLException {
      DriverManager.registerDriver( 
         new oracle.jdbc.driver.OracleDriver()
      );  
      return DriverManager.getConnection( url, user, pw );
   }
	
   private static String url = "jdbc:oracle:thin:@localhost:1521:db";
   private static String user = "report";
   private static String pw   = "report";
}



需要的包:
jasperreports-3.1.2.jar
jasperreports-3.1.2-applet.jar
jasperreports-3.1.2-javaflow.jar
commons-collections.jar
commons-digester.jar
commons-logging-1.0.4.jar
commons-logging-api.jar
commons-beanutils.jar
itext-1.3.1.jar
classes12.jar
poi-3.0.1.jar
poi-contrib-3.0.1-FINAL-20070705.jar
poi-scratchpad-3.0.1-FINAL-20070705.jar

文件结构:
+test
++report
+++GenerateReport.java

+reports
++Location.jrxml
++Location.jasper

数据表要和Location.jrmal或Location.jasper(即report的设计一致就行了)

另外可以参考一下这里:
http://blog.csdn.net/kabini/archive/2007/05/08/1600098.aspx

分享到:
评论
2 楼 iris_1992 2015-04-30  
2005年以前,国外开原报表完全碾压国产软件,但是现在国内软件,像帆软、FineBI,都比较牛掰了,设计模式和数据处理方面优于开源报表,真犯不着再用Jasperreport了,
1 楼 niweiwei 2010-12-23  
JasperCompileManager.compileReport("reports/Location.jrxml");  
在编译的时候报net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException:
不知道只怎么回事,麻烦楼主帮个忙 谢谢

相关推荐

Global site tag (gtag.js) - Google Analytics