Jasperreport的一些使用技巧

类别:Java 点击:0 评论:0 推荐:

1. ireport生成中文PDF报表时,需设置:
 1)增加itext中文包在lib目录下
 2)pdf encoding 选 UniGB-UCS2-H (Chinese Simplified)
   pdf font name 选 stsong-light
   (可以使用View->Report Fonts一次解决)
2. ireport有时侯不能显示数据库中数据的解决方式:
  1)每一个element:设置:print when details overflow ;stretch overflow
  2)字段高度不够

3. dori.jasper.engine.JRException: Error loading object
  from file : D:\jakarta-tomcat-4.0.1\jakarta-tomcat-4.0.1\webapps\safe\safe1.jasper

 新的jasperreport的版本是0.5.2,ireport用的是0.5.0,你把你的搜索路径里面的库换一下就好了

4. 一段测试代码:
  package test;

  /**
   * <p>Title: </p>
   * <p>Description: </p>
   * <p>Copyright: Copyright (c) 2003</p>
   * <p>Company: est</p>
   * @author mgp
   * @version 1.0
   */
  import dori.jasper.engine.*;
  import dori.jasper.engine.util.*;
  import java.sql.*;
  import java.util.*;
  import dori.jasper.engine.JasperReport;
  import java.util.Date;
  import java.text.SimpleDateFormat;
  
  public class report {
    public report() {
    }
  
    public static void main(String[] args){
  
      JRResultSetDataSource jrds=null;
      JasperPrint jasperPrint=null;
      Map reportParams = new HashMap();
      ResultSet rs=null;
      Connection con = null;
      Statement stmt = null;
      String sql = "select * from wms_sku";
      String driver = "oracle.jdbc.driver.OracleDriver";
      String url = "jdbc:oracle:thin:@192.168.0.254:1521:server1";
  
  
      try {
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(
            "C:/est/helix/helix/reports/testdao.jasper");
        Class.forName(driver);
        con = DriverManager.getConnection(url, "****", "****");
        stmt = con.createStatement();
        rs = stmt.executeQuery(sql);
        if(!rs.next())
          return ;
        jrds = new JRResultSetDataSource(rs);
        jasperPrint =
          JasperFillManager.fillReport(
                                      jasperReport,
                                      reportParams,
                                      jrds
          );
  
  
        JasperPrintManager.printReportToPdfFile(jasperPrint, "c:/abcd.pdf");
      }
      catch (ClassNotFoundException ex) {
        ex.printStackTrace();
      }
      catch (SQLException ex) {
        ex.printStackTrace();
      }
      catch (JRException e) {
          e.printStackTrace();
      }
    }
  }

5. 利用mysql数据库,生成pdf报表,在Tomcat中显示:
 
 <%@ page import="dori.jasper.engine.*" %>
 <%@ page import="java.util.*" %>
 <%@ page import="java.io.*" %>
 <%@ page import="java.sql.*" %>
 <%
  File reportFile = new File(application.getRealPath("/reports/mydoc5.jasper"));
  Map parameters = new HashMap();
  
  Class.forName("org.gjt.mm.mysql.Driver").newInstance();
  String url ="jdbc:mysql://localhost/clients?user=sa&password=&useUnicode=true&characterEncoding=GB2312";
  Connection conn= DriverManager.getConnection(url);
 
      
  byte[] bytes =
   JasperRunManager.runReportToPdf(
    reportFile.getPath(),
    parameters,
    conn
    );
  
  response.setContentType("application/pdf");
  response.setContentLength(bytes.length);
  ServletOutputStream ouputStream = response.getOutputStream();
  ouputStream.write(bytes, 0, bytes.length);
  ouputStream.flush();
  ouputStream.close();
    
 %>
     note:
     1)<%@ page contentType="text/html;charset=gb2312"%> 这句话不能有,不然会提示下载
     2)从数据库中获取中文数据Encoding=GB2312是必须的
     3)由于ireport使用的jasperreport版本与jasperreport发行的版本不一致,所以会造成很多错误,例如:
      dori.jasper.engine.JRException: Error loading object
       from file : D:\jakarta-tomcat-4.0.1\jakarta-tomcat-4.0.1\webapps\safe\safe1.jasper

本文地址:http://com.8s8s.com/it/it15123.htm