国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
JasperReport報(bào)表設(shè)計(jì)總結(jié)

為了開(kāi)發(fā)報(bào)表,已經(jīng)拜讀了一大堆的資料,其中大部分是重復(fù)的??梢钥吹贸?,國(guó)人還是比較熱衷于copy&paste的工作。所以找了一大堆的資料都是相同的,或者可以用一個(gè)詞來(lái)形容,換湯不換藥的。
有必要對(duì)于jasper Report的學(xué)習(xí)進(jìn)度進(jìn)行一下總結(jié),一來(lái)可以更新一下以前的資料中的一些舊得不能再舊的不再適用的東西,二來(lái)讓后來(lái)者可以輕松的上手。
首先,jasperReport是一個(gè)以java實(shí)現(xiàn)的報(bào)表工具,(好像是句廢話)??梢詫?shí)現(xiàn)多種格式的報(bào)表。
再我們先理解一下jasperReport的實(shí)現(xiàn)過(guò)程。筆者不再畫(huà)圖,網(wǎng)上到處都是。
1)定制報(bào)表格式。
  有二種方式,一種就是寫(xiě)jrxml文件,其實(shí)就是xml文件,只不過(guò)是后綴名不一樣罷了。另一種方式更直接,就是生成一個(gè)JasperDesign類(lèi)的實(shí)例,在japsperDesign中自己定義模板。jrxml文件也是通過(guò)一個(gè)JRXmlLoad加載過(guò)來(lái),轉(zhuǎn)成JasperDesign類(lèi)的實(shí)例。也就是說(shuō)寫(xiě)jrxml文件還需要進(jìn)行解析,加載?,F(xiàn)實(shí)中我們使用的報(bào)表一般格式比較固定,因而可以通過(guò)先使用iReport工具生成模板,再加載解析的方式。這種方式簡(jiǎn)單,而且可見(jiàn)性強(qiáng)。
2)填充數(shù)據(jù)。
  在最新版(2007.4.30發(fā)布)的jasperReports1.3.3中,支持了很多的格式,包括了對(duì)于Hibernate的支持。填充數(shù)據(jù)對(duì)于我們經(jīng)常使用的來(lái)說(shuō),一般是二種方式,一種方式是通過(guò)JDBC連接提供數(shù)據(jù)源,一種就是通過(guò)javaBean的集合提供數(shù)據(jù)源。當(dāng)然還有web Service的xml文件提供的。我的建議是,如果你的程序中的統(tǒng)計(jì)直接使用Jdbc就可以完成,那么就使用jdbc數(shù)據(jù)源的方法,反之,使用javaBean的集合是不錯(cuò)的選擇,因?yàn)檫@樣不會(huì)在意你的數(shù)據(jù)的來(lái)源,你也可以任意處理,比如說(shuō),要通過(guò)權(quán)限檢查的數(shù)據(jù)才在報(bào)表中生成的話,就可以過(guò)濾到不符合權(quán)限的數(shù)據(jù)。
3)顯示或打印。
  顯示,即將JasperReport生成的文件直接顯示出來(lái),打印所使用的方式其實(shí)就是生成文件,然后調(diào)用打印來(lái)對(duì)文件進(jìn)行打印。
看起來(lái)是不是很簡(jiǎn)單,對(duì),確實(shí)很簡(jiǎn)單。
筆者主要從事基于B/S模式的開(kāi)發(fā),相信大部分人的使用也是基于web進(jìn)行使用。故筆者對(duì)于基于web的開(kāi)發(fā)進(jìn)行詳細(xì)講述。

1、工程環(huán)境配置。
將以下包加入WEB-INF/lib中。
commons-beanutils-1.7.jar;commons-collections-2.1.jar;commons-digester-1.7.jar;commons-logging-1.0.2.jar;commons-logging-api-1.0.2.jar;itext-1.3.1.jar;jasperreports-1.3.3.jar;jdt-compiler-3.1.1.jar;jxl-2.6.jar;png-encoder-1.5.jar;poi-2.0-final-20040126.jar
   以上包是jasperReport必須。
2、iReport的安裝
   這個(gè)很簡(jiǎn)單,直接下一步就可以了。
3、使用iReport生成 .jasper文件
.jasper是經(jīng)過(guò)編譯的japserReport模板,即.jrxml編譯后的。
對(duì)于iReport的使用網(wǎng)絡(luò)上已經(jīng)有了詳細(xì)的中文的文檔,具體可以下載附件查看。使用.jasper文件可以避免在系統(tǒng)中再進(jìn)行編譯,增加系統(tǒng)的壓力。
4、數(shù)據(jù)填充
數(shù)據(jù)填充是相對(duì)于比較重要的一步,數(shù)據(jù)填充的目的是為了將數(shù)據(jù)填充進(jìn)去,生成一個(gè)JapserPrint對(duì)象。筆者在之前已經(jīng)闡述過(guò),現(xiàn)在主要是基于關(guān)系型數(shù)據(jù)庫(kù)(比較簡(jiǎn)單的,沒(méi)有權(quán)限控制的),以及基于JavaBean Collection進(jìn)行數(shù)據(jù)填充。進(jìn)行數(shù)據(jù)填充的基本就是一定要實(shí)現(xiàn)JRDataSource,JRDataSource定義了二個(gè)方法,一個(gè)是指針移動(dòng)的方法next(),另一個(gè)是取值的getFieldValue()方法,用來(lái)取值填充。
對(duì)于關(guān)系型數(shù)據(jù)庫(kù),即直接利用數(shù)據(jù)庫(kù)的表來(lái)生成報(bào)表的,只需要傳入一個(gè)Java Connection即可。JasperReport已經(jīng)有了個(gè)默認(rèn)的實(shí)現(xiàn)的JRDataSource的接口了。對(duì)于Java Conncetion的獲得,筆者建議使用工廠方法或者使用Spring方式來(lái)獲得。可以參照筆者寫(xiě)的如下:

java 代碼
 
  1. /**  
  2.  * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.  
  3.  * @author Jimmy.Shine 2007-5-11  
  4.  */  
  5. package cn.com.reachway.framework.report;   
  6.   
  7. import java.sql.Connection;   
  8. import java.sql.DriverManager;   
  9.   
  10. import cn.com.reachway.framework.exception.JasperReportException;   
  11.   
  12. /**  
  13.  * Used for get a JDBC connection for Jasper Report  
  14.  */  
  15. public class JDBCConnection {   
  16.  /**  
  17.   * Which Jdbc driver were used. Like: "net.sourceforge.jtds.jdbc.Driver"  
  18.   */  
  19.  private String jdbcDriver;   
  20.  /**  
  21.   * The jdbc url,define which database server,which db. Like:  
  22.   * "jdbc:jtds:sqlserver://192.168.1.50:1433;DatabaseName=JAVA5;SelectMethod=Cursor"  
  23.   */  
  24.  private String jdbcUrl;   
  25.   
  26.  private String dbUser;   
  27.   
  28.  private String dbPassword;   
  29.   
  30.  public String getDbPassword() {   
  31.   return dbPassword;   
  32.  }   
  33.   
  34.  public void setDbPassword(String dbPassword) {   
  35.   this.dbPassword = dbPassword;   
  36.  }   
  37.   
  38.  public String getDbUser() {   
  39.   return dbUser;   
  40.  }   
  41.   
  42.  public void setDbUser(String dbUser) {   
  43.   this.dbUser = dbUser;   
  44.  }   
  45.   
  46.  public String getJdbcDriver() {   
  47.   return jdbcDriver;   
  48.  }   
  49.   
  50.  public void setJdbcDriver(String jdbcDriver) {   
  51.   this.jdbcDriver = jdbcDriver;   
  52.  }   
  53.   
  54.  public String getJdbcUrl() {   
  55.   return jdbcUrl;   
  56.  }   
  57.   
  58.  public void setJdbcUrl(String jdbcUrl) {   
  59.   this.jdbcUrl = jdbcUrl;   
  60.  }   
  61.   
  62.  public JDBCConnection() {   
  63.   super();   
  64.  }   
  65.  /**  
  66.   * Get the Connection   
  67.   * @return connection  
  68.   * @throws JasperReportException  
  69.   */  
  70.  public Connection getConnection() throws JasperReportException {   
  71.   Connection con;   
  72.   try {   
  73.    check();   
  74.    Class.forName(this.jdbcDriver);   
  75.    con = DriverManager.getConnection(this.jdbcUrl, this.dbUser, this.dbPassword);   
  76.    return con;   
  77.   } catch (Exception e) {   
  78.    e.printStackTrace();   
  79.    throw new JasperReportException("Get JDBC Connection Error");   
  80.   }   
  81.  }   
  82.     
  83.  /**  
  84.   * Check the Configure  
  85.   * @throws JasperReportException  
  86.   */  
  87.  private void check() throws JasperReportException {   
  88.   if (this.jdbcDriver == null || this.jdbcDriver.equals("") || this.jdbcUrl == null || this.jdbcUrl.equals("")   
  89.     || this.dbUser == null || this.dbUser.equals("") || this.dbPassword == null) {   
  90.    throw new JasperReportException("Jdbc Configure error!");   
  91.   }   
  92.  }   
  93.   
  94. }   
  95.   


java 代碼
 
  1. /**  
  2.  * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.  
  3.  * @author Jimmy.Shine 2007-5-14  
  4.  */  
  5. package cn.com.reachway.framework.report.dataSource;   
  6.   
  7. import net.sf.jasperreports.engine.JRDataSource;   
  8. import net.sf.jasperreports.engine.JRException;   
  9. import net.sf.jasperreports.engine.JRField;   
  10.   
  11. /**  
  12.  * 報(bào)表的dataSource的基類(lèi),所有利用DataSource產(chǎn)生報(bào)表的繼承此類(lèi)  
  13.  */  
  14. public abstract class ReportDataSource implements JRDataSource {   
  15.   
  16.  /**  
  17.   * 取值  
  18.   */  
  19.  public abstract Object getFieldValue(JRField jrField) throws JRException;   
  20.   
  21.  /**  
  22.   * 指針移動(dòng)  
  23.   */  
  24.  public abstract boolean next() throws JRException;   
  25.   
  26. }   
  27.   
  這樣可以共用配置文件中的jdbc連接的相關(guān)參數(shù)。
對(duì)于其它的數(shù)據(jù)源,JavaBean Collection,筆者采用了一個(gè)抽象類(lèi),使用抽象類(lèi)的目的,是為了使開(kāi)發(fā)者盡可能不用直接接觸jasperReport(此為筆者自己的方式,筆者是PM,為了考慮底下的人的開(kāi)發(fā))。以下為抽象類(lèi)以及筆者提供的一個(gè)sample:
 
  1. /**  
  2.  * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.  
  3.  * @author Jimmy.Shine 2007-5-14  
  4.  */  
  5. package cn.com.reachway.framework.report.dataSource;   
  6.   
  7. import java.util.ArrayList;   
  8. import java.util.List;   
  9.   
  10. import net.sf.jasperreports.engine.JRException;   
  11. import net.sf.jasperreports.engine.JRField;   
  12.   
  13. /**  
  14.  * 利用JavaBean Collection生成ReportDataSource的例子  
  15.  */  
  16. public class ReportDataSourceSample extends ReportDataSource {   
  17.   
  18.  private List docs = new ArrayList();   
  19.   
  20.  private int index = -1;   
  21.   
  22.  public ReportDataSourceSample() {   
  23.   for (int i = 0; i < 10; i++) {   
  24.    Document doc = new Document("ViewTimes is:" + i, i);   
  25.    this.docs.add(doc);   
  26.   }   
  27.  }   
  28.   
  29.  // 內(nèi)部類(lèi),作為demo時(shí)使用   
  30.  private class Document {   
  31.   private String name;   
  32.   
  33.   private int viewTimes;   
  34.   
  35.   public String getName() {   
  36.    return name;   
  37.   }   
  38.   
  39.   public void setName(String name) {   
  40.    this.name = name;   
  41.   }   
  42.   
  43.   public int getViewTimes() {   
  44.    return viewTimes;   
  45.   }   
  46.   
  47.   public void setViewTimes(int viewTimes) {   
  48.    this.viewTimes = viewTimes;   
  49.   }   
  50.   
  51.   public Document() {   
  52.   }   
  53.   
  54.   public Document(String name, int viewTimes) {   
  55.    this.name = name;   
  56.    this.viewTimes = viewTimes;   
  57.   }   
  58.  }   
  59.   
  60.  public List getDocs() {   
  61.   return docs;   
  62.  }   
  63.   
  64.  public void setDocs(List docs) {   
  65.   this.docs = docs;   
  66.  }   
  67.   
  68.  @Override  
  69.  public Object getFieldValue(JRField jrField) throws JRException {   
  70.   
  71.   String fieldName = jrField.getName();   
  72.   Document doc = this.docs.get(index);   
  73.   if ("name".equals(fieldName)) {   
  74.    return doc.getName();   
  75.   }   
  76.   if ("viewTimes".equals(fieldName)) {   
  77.    return doc.getViewTimes();   
  78.   }   
  79.   return null;   
  80.  }   
  81.   
  82.  @Override  
  83.  public boolean next() throws JRException {   
  84.   index++;   
  85.   return (index < this.docs.size());   
  86.  }   
  87. }   
  88.   
java 代碼

以上的例子應(yīng)當(dāng)很清楚的寫(xiě)明了如何生成數(shù)據(jù)源。
對(duì)于數(shù)據(jù)源的填充,筆者使用了二個(gè)類(lèi),分別用來(lái)對(duì)應(yīng)使用Connction及JavaBean Collection進(jìn)行填充。

java 代碼
 
  1. /**  
  2.  * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.  
  3.  * @author Jimmy.Shine 2007-5-12  
  4.  */  
  5. package cn.com.reachway.framework.report.jasperPrint;   
  6.   
  7. import java.io.File;   
  8. import java.sql.Connection;   
  9. import java.util.Map;   
  10.   
  11. import net.sf.jasperreports.engine.JRException;   
  12. import net.sf.jasperreports.engine.JasperFillManager;   
  13. import net.sf.jasperreports.engine.JasperPrint;   
  14. import net.sf.jasperreports.engine.JasperReport;   
  15. import net.sf.jasperreports.engine.util.JRLoader;   
  16. import cn.com.reachway.framework.exception.JasperReportException;   
  17.   
  18. /**  
  19.  * 使用報(bào)表模板及數(shù)據(jù)等來(lái)生成JapserPrint  
  20.  */  
  21. public class JasperPrintWithConnection {   
  22.  /** 傳入的參數(shù) */  
  23.  private Map params;   
  24.  /** 模板文件的地址 */  
  25.  private String reportFilePath;   
  26.  /** JDBC connection */  
  27.  private Connection con;   
  28.   
  29.  public Connection getCon() {   
  30.   return con;   
  31.  }   
  32.   
  33.  public void setCon(Connection con) {   
  34.   this.con = con;   
  35.  }   
  36.   
  37.  public Map getParams() {   
  38.   return params;   
  39.  }   
  40.   
  41.  public void setParams(Map params) {   
  42.   this.params = params;   
  43.  }   
  44.   
  45.  public String getReportFilePath() {   
  46.   return reportFilePath;   
  47.  }   
  48.   
  49.  public void setReportFilePath(String reportFilePath) throws JasperReportException {   
  50.   if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))   
  51.    throw new JasperReportException("您傳入的模板文件格式不對(duì),請(qǐng)傳入以.jasper為后綴的文件!");   
  52.   this.reportFilePath = reportFilePath;   
  53.  }   
  54.   
  55.  public JasperPrintWithConnection() {   
  56.   super();   
  57.  }   
  58.   
  59.  public JasperPrintWithConnection(String reportFilePath, Map params, Connection con) throws JasperReportException {   
  60.   if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))   
  61.    throw new JasperReportException("模板文件格式不對(duì),請(qǐng)傳入以.jasper為后綴的文件!");   
  62.   if (con == null)   
  63.    throw new JasperReportException("Conncetion不應(yīng)當(dāng)為null!");   
  64.   this.setReportFilePath(reportFilePath);   
  65.   this.setParams(params);   
  66.   this.setCon(con);   
  67.  }   
  68.     
  69.  /**  
  70.   * 取得JasperPrint  
  71.   * @return  
  72.   * @throws JasperReportException  
  73.   */  
  74.  public JasperPrint getJasperPrint() throws JasperReportException {   
  75.   File reportFile = new File(this.reportFilePath);   
  76.   if (!reportFile.exists())   
  77.    throw new JasperReportException("傳入的模板文件不存在!");   
  78.   
  79.   try {   
  80.    // Load編譯好的模板   
  81.    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());   
  82.    // 進(jìn)行數(shù)據(jù)填充   
  83.    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, this.params, this.con);   
  84.    return jasperPrint;   
  85.   
  86.   } catch (JRException jre) {   
  87.    jre.printStackTrace();   
  88.    throw new JasperReportException("在進(jìn)行數(shù)據(jù)填充時(shí)發(fā)生了錯(cuò)誤中,請(qǐng)檢查是否是數(shù)據(jù)庫(kù)連接錯(cuò)誤或者是用來(lái)填充的參數(shù)map有誤!");   
  89.   }   
  90.   
  91.  }   
  92. }   
  93.   
 
  1. /**  
  2.  * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd.  
  3.  * @author Jimmy.Shine 2007-5-14  
  4.  */  
  5. package cn.com.reachway.framework.report.jasperPrint;   
  6.   
  7. import java.io.File;   
  8. import java.util.Map;   
  9.   
  10. import net.sf.jasperreports.engine.JRDataSource;   
  11. import net.sf.jasperreports.engine.JRException;   
  12. import net.sf.jasperreports.engine.JasperFillManager;   
  13. import net.sf.jasperreports.engine.JasperPrint;   
  14. import net.sf.jasperreports.engine.JasperReport;   
  15. import net.sf.jasperreports.engine.util.JRLoader;   
  16. import cn.com.reachway.framework.exception.JasperReportException;   
  17.   
  18. /**  
  19.  *   
  20.  */  
  21. public class JasperPrintWithDataSource {   
  22.  /** 傳入的參數(shù) */  
  23.  private Map params;   
  24.  /** 模板文件的地址 */  
  25.  private String reportFilePath;   
  26.  /** dataSrouce */  
  27.  private JRDataSource dataSource;   
  28.   
  29.  public JRDataSource getDataSource() {   
  30.   return dataSource;   
  31.  }   
  32.   
  33.  public void setDataSource(JRDataSource dataSource) {   
  34.   this.dataSource = dataSource;   
  35.  }   
  36.   
  37.  public Map getParams() {   
  38.   return params;   
  39.  }   
  40.   
  41.  public void setParams(Map params) {   
  42.   this.params = params;   
  43.  }   
  44.   
  45.  public String getReportFilePath() {   
  46.   return reportFilePath;   
  47.  }   
  48.   
  49.  public void setReportFilePath(String reportFilePath) throws JasperReportException {   
  50.   if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))   
  51.    throw new JasperReportException("您傳入的模板文件格式不對(duì),請(qǐng)傳入以.jasper為后綴的文件!");   
  52.   this.reportFilePath = reportFilePath;   
  53.  }   
  54.   
  55.  public JasperPrintWithDataSource() {   
  56.   super();   
  57.  }   
  58.   
  59.  public JasperPrintWithDataSource(String reportFilePath, Map params, JRDataSource dataSource)   
  60.    throws JasperReportException {   
  61.   if (reportFilePath == null || !reportFilePath.endsWith(".jasper"))   
  62.    throw new JasperReportException("模板文件格式不對(duì),請(qǐng)傳入以.jasper為后綴的文件!");   
  63.   if (dataSource == null)   
  64.    throw new JasperReportException("DataSource不應(yīng)當(dāng)為null!");   
  65.   this.setReportFilePath(reportFilePath);   
  66.   this.setParams(params);   
  67.   this.setDataSource(dataSource);   
  68.  }   
  69.  /**  
  70.   * 取得JasperPrint  
  71.   * @return  
  72.   * @throws JasperReportException  
  73.   */  
  74.  public JasperPrint getJasperPrint() throws JasperReportException {   
  75.   File reportFile = new File(this.reportFilePath);   
  76.   if (!reportFile.exists())   
  77.    throw new JasperReportException("傳入的模板文件不存在!");   
  78.   
  79.   try {   
  80.    // Load編譯好的模板   
  81.    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());   
  82.    // 進(jìn)行數(shù)據(jù)填充   
  83.    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, this.params, this.dataSource);   
  84.    return jasperPrint;   
  85.   
  86.   } catch (JRException jre) {   
  87.    jre.printStackTrace();   
  88.    throw new JasperReportException("在進(jìn)行數(shù)據(jù)填充時(shí)發(fā)生了錯(cuò)誤中,請(qǐng)檢查是否是數(shù)據(jù)庫(kù)連接錯(cuò)誤或者是用來(lái)填充的參數(shù)map有誤!");   
  89.   }   
  90.   
  91.  }   
  92. }   
  93.   
java 代碼

其中使用的JasperReportException為筆者定義的異常,用來(lái)統(tǒng)一處理報(bào)表異常。

5、報(bào)表產(chǎn)生
報(bào)表產(chǎn)生是程序中最終可見(jiàn)的一部分,在jasperReport的demo中,大部分中使用了jasperReport的net.sf.jasperreports.j2ee.servlets.*中的類(lèi)來(lái)生成。其實(shí)這也算是開(kāi)源的產(chǎn)品的一個(gè)問(wèn)題,其實(shí)jasperReport提供的report的工具,只能算是demo級(jí)別的,不能算是產(chǎn)品級(jí)別的。相信很多的朋友在使用的時(shí)候就碰上無(wú)法生成的問(wèn)題。筆者在此基礎(chǔ)上封裝了一下。

由于在一個(gè)里面不能貼太多的代碼,故放置代碼在此處。
上接:http://jimmy-shine.javaeye.com/blog/78678
下接:http://jimmy-shine.javaeye.com/blog/123597

java 代碼
 
  1. /** 
  2.  * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd. 
  3.  * @author Jimmy.Shine 2007-5-12 
  4.  */  
  5. package cn.com.reachway.framework.report.export;  
  6.   
  7. import java.io.IOException;  
  8. import java.io.PrintWriter;  
  9. import java.sql.Connection;  
  10. import java.util.Map;  
  11.   
  12. import javax.servlet.http.HttpServletRequest;  
  13. import javax.servlet.http.HttpServletResponse;  
  14.   
  15. import net.sf.jasperreports.engine.JRDataSource;  
  16. import net.sf.jasperreports.engine.JRExporterParameter;  
  17. import net.sf.jasperreports.engine.JasperPrint;  
  18. import net.sf.jasperreports.engine.export.JRHtmlExporter;  
  19. import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;  
  20. import net.sf.jasperreports.j2ee.servlets.ImageServlet;  
  21. import cn.com.reachway.framework.exception.JasperReportException;  
  22. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithConnection;  
  23. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithDataSource;  
  24.   
  25. /** 
  26.  * 利用報(bào)表生成HTML格式報(bào)表 
  27.  */  
  28. public class HTMLExport {  
  29.   
  30.     /** 
  31.      * 導(dǎo)出報(bào)表 
  32.      *  
  33.      * @param request 
  34.      * @param response 
  35.      * @param reportFilePath 
  36.      * @param params 
  37.      * @param con 
  38.      * @throws JasperReportException 
  39.      */  
  40.     public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,  
  41.             Connection con) throws JasperReportException {  
  42.         try {  
  43.             PrintWriter out = response.getWriter();  
  44.             try {  
  45.                 response.setContentType("text/html;charset=UTF-8");  
  46.                 JasperPrint jasperPrint = new JasperPrintWithConnection(reportFilePath, params, con).getJasperPrint();  
  47.                 // 使用JRHtmlExproter導(dǎo)出Html格式  
  48.                 JRHtmlExporter exporter = new JRHtmlExporter();  
  49.                 request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);  
  50.                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);  
  51.                 exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);  
  52.                 exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "./servlets/image?image=");  
  53.                 exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");  
  54.                 // 導(dǎo)出  
  55.                 exporter.exportReport();  
  56.             } catch (Exception e) {  
  57.                 e.printStackTrace();  
  58.                 throw new JasperReportException("在導(dǎo)出Html格式報(bào)表時(shí)發(fā)生錯(cuò)誤!");  
  59.             } finally {  
  60.                 if (out != null) {  
  61.                     try {  
  62.                         out.close();  
  63.                     } catch (Exception e) {  
  64.                     }  
  65.                 }  
  66.             }  
  67.         } catch (IOException ioe) {  
  68.             ioe.printStackTrace();  
  69.             throw new JasperReportException("從Response中取得PrintWriter時(shí)發(fā)生錯(cuò)誤!");  
  70.         }  
  71.     }  
  72.   
  73.     /** 
  74.      * 導(dǎo)出報(bào)表 
  75.      *  
  76.      * @param request 
  77.      * @param response 
  78.      * @param reportFilePath 
  79.      * @param params 
  80.      * @param dataSource 
  81.      * @throws JasperReportException 
  82.      */  
  83.     public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,  
  84.             JRDataSource dataSource) throws JasperReportException {  
  85.         try {  
  86.             PrintWriter out = response.getWriter();  
  87.             try {  
  88.                 response.setContentType("text/html;charset=UTF-8");  
  89.                 JasperPrint jasperPrint = new JasperPrintWithDataSource(reportFilePath, params, dataSource)  
  90.                         .getJasperPrint();  
  91.                 // 使用JRHtmlExproter導(dǎo)出Html格式  
  92.                 JRHtmlExporter exporter = new JRHtmlExporter();  
  93.                 request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);  
  94.                 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);  
  95.                 exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);  
  96.                 exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "./servlets/image?image=");  
  97.                 exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");  
  98.                 // 導(dǎo)出  
  99.                 exporter.exportReport();  
  100.             } catch (Exception e) {  
  101.                 e.printStackTrace();  
  102.                 throw new JasperReportException("在導(dǎo)出Html格式報(bào)表時(shí)發(fā)生錯(cuò)誤!");  
  103.             } finally {  
  104.                 if (out != null) {  
  105.                     try {  
  106.                         out.close();  
  107.                     } catch (Exception e) {  
  108.                     }  
  109.                 }  
  110.             }  
  111.         } catch (IOException ioe) {  
  112.             ioe.printStackTrace();  
  113.             throw new JasperReportException("從Response中取得PrintWriter時(shí)發(fā)生錯(cuò)誤!");  
  114.         }  
  115.     }  
  116.   
  117. }  

Excel格式的:

java 代碼
 
  1. /** 
  2.  * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd. 
  3.  * @author Jimmy.Shine 2007-5-13 
  4.  */  
  5. package cn.com.reachway.framework.report.export;  
  6.   
  7. import java.io.IOException;  
  8. import java.io.OutputStream;  
  9. import java.net.URLEncoder;  
  10. import java.sql.Connection;  
  11. import java.util.List;  
  12. import java.util.Map;  
  13.   
  14. import javax.servlet.http.HttpServletRequest;  
  15. import javax.servlet.http.HttpServletResponse;  
  16.   
  17. import net.sf.jasperreports.engine.JRDataSource;  
  18. import net.sf.jasperreports.engine.JRException;  
  19. import net.sf.jasperreports.engine.JRExporterParameter;  
  20. import net.sf.jasperreports.engine.JasperPrint;  
  21. import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;  
  22. import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;  
  23. import net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;  
  24. import cn.com.reachway.framework.exception.JasperReportException;  
  25. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithConnection;  
  26. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithDataSource;  
  27.   
  28. /** 
  29.  * 利用報(bào)表生成Xls的基類(lèi) 
  30.  */  
  31. public abstract class BaseExcelExport {  
  32.     /** 
  33.      * 導(dǎo)出報(bào)表 
  34.      *  
  35.      * @param request 
  36.      * @param response 
  37.      * @param reportFilePath 
  38.      * @param params 
  39.      * @param con 
  40.      * @param fileName 
  41.      * @throws JasperReportException 
  42.      */  
  43.     public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,  
  44.             Connection con, String fileName) throws JasperReportException {  
  45.         JasperPrint jasperPrint = new JasperPrintWithConnection(reportFilePath, params, con).getJasperPrint();  
  46.         // 將填充完的japserPrint放入session中。  
  47.         request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);  
  48.         // 拿到j(luò)apserPrintList  
  49.         List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);  
  50.         // 若沒(méi)有JasperPrintList,則拋出異常  
  51.         if (jasperPrintList == null) {  
  52.             throw new JasperReportException("在Http Session中沒(méi)有找到JasperPrint List");  
  53.         }  
  54.         try {  
  55.             OutputStream ouputStream = response.getOutputStream();  
  56.             try {  
  57.   
  58.                 response.setContentType("application/xls");  
  59.                 response.setCharacterEncoding("UTF-8");  
  60.                 if (fileName == null || fileName.equals(""))  
  61.                     response.setHeader("Content-Disposition""inline; filename=\"noTitle.xls\"");  
  62.                 else {  
  63.                     response.setHeader("Content-Disposition""inline; filename=\""  
  64.                             + URLEncoder.encode(fileName, "UTF-8") + ".xls\"");  
  65.   
  66.                 }  
  67.                 // Xls格式的導(dǎo)出器 JRXlsAbstractExport  
  68.                 JRXlsAbstractExporter exporter = getXlsExporter();  
  69.   
  70.                 // 在導(dǎo)出器中放入要導(dǎo)出的japserPrintList  
  71.                 exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);  
  72.   
  73.                 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);  
  74.                 // 設(shè)置Xls的屬性  
  75.                 exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);  
  76.                 exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);  
  77.                 // 導(dǎo)出  
  78.                 exporter.exportReport();  
  79.             } catch (JRException e) {  
  80.                 e.printStackTrace();  
  81.                 throw new JasperReportException("在生成XLS報(bào)表時(shí)發(fā)生錯(cuò)誤!");  
  82.             }  
  83.   
  84.             finally {  
  85.                 if (ouputStream != null) {  
  86.                     try {  
  87.                         ouputStream.close();  
  88.                     } catch (IOException ex) {  
  89.                     }  
  90.                 }  
  91.             }  
  92.         } catch (IOException ioe) {  
  93.             ioe.printStackTrace();  
  94.             throw new JasperReportException("從Response中取得OutputStream時(shí)發(fā)生錯(cuò)誤!");  
  95.         }  
  96.   
  97.     }  
  98.   
  99.     /** 
  100.      * 導(dǎo)出報(bào)表 
  101.      *  
  102.      * @param request 
  103.      * @param response 
  104.      * @param reportFilePath 
  105.      * @param params 
  106.      * @param dataSource 
  107.      * @param fileName 
  108.      * @throws JasperReportException 
  109.      */  
  110.     public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,  
  111.             JRDataSource dataSource, String fileName) throws JasperReportException {  
  112.         JasperPrint jasperPrint = new JasperPrintWithDataSource(reportFilePath, params, dataSource).getJasperPrint();  
  113.         // 將填充完的japserPrint放入session中。  
  114.         request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);  
  115.         // 拿到j(luò)apserPrintList  
  116.         List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);  
  117.         // 若沒(méi)有JasperPrintList,則拋出異常  
  118.         if (jasperPrintList == null) {  
  119.             throw new JasperReportException("在Http Session中沒(méi)有找到JasperPrint List");  
  120.         }  
  121.         try {  
  122.             OutputStream ouputStream = response.getOutputStream();  
  123.             try {  
  124.   
  125.                 response.setContentType("application/xls");  
  126.                 response.setCharacterEncoding("UTF-8");  
  127.                 if (fileName == null || fileName.equals(""))  
  128.                     response.setHeader("Content-Disposition""inline; filename=\"noTitle.xls\"");  
  129.                 else {  
  130.                     response.setHeader("Content-Disposition""inline; filename=\""  
  131.                             + URLEncoder.encode(fileName, "UTF-8") + ".xls\"");  
  132.   
  133.                 }  
  134.                 // Xls格式的導(dǎo)出器 JRXlsAbstractExport  
  135.                 JRXlsAbstractExporter exporter = getXlsExporter();  
  136.   
  137.                 // 在導(dǎo)出器中放入要導(dǎo)出的japserPrintList  
  138.                 exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);  
  139.   
  140.                 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);  
  141.                 // 設(shè)置Xls的屬性  
  142.                 exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);  
  143.                 exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);  
  144.                 // 導(dǎo)出  
  145.                 exporter.exportReport();  
  146.             } catch (JRException e) {  
  147.                 e.printStackTrace();  
  148.                 throw new JasperReportException("在生成XLS報(bào)表時(shí)發(fā)生錯(cuò)誤!");  
  149.             }  
  150.   
  151.             finally {  
  152.                 if (ouputStream != null) {  
  153.                     try {  
  154.                         ouputStream.close();  
  155.                     } catch (IOException ex) {  
  156.                     }  
  157.                 }  
  158.             }  
  159.         } catch (IOException ioe) {  
  160.             ioe.printStackTrace();  
  161.             throw new JasperReportException("從Response中取得OutputStream時(shí)發(fā)生錯(cuò)誤!");  
  162.         }  
  163.   
  164.     }  
  165.   
  166.     protected abstract JRXlsAbstractExporter getXlsExporter();  
  167.   
  168. }  
java 代碼
 
  1. /** 
  2.  * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd. 
  3.  * @author Jimmy.Shine 2007-5-13 
  4.  */  
  5. package cn.com.reachway.framework.report.export;  
  6.   
  7. import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;  
  8. import net.sf.jasperreports.engine.export.JRXlsExporter;  
  9.   
  10. /** 
  11.  * 利用報(bào)表使用POI生成XLS報(bào)表 
  12.  */  
  13. public class XlsPOIExport extends BaseExcelExport {  
  14.   
  15.     protected JRXlsAbstractExporter getXlsExporter() {  
  16.         return new JRXlsExporter();  
  17.     }  
  18.   
  19. }  
  20.     
java 代碼
 
  1. /** 
  2.  * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd. 
  3.  * @author Jimmy.Shine 2007-5-13 
  4.  */  
  5. package cn.com.reachway.framework.report.export;  
  6.   
  7. import net.sf.jasperreports.engine.export.JExcelApiExporter;  
  8. import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;  
  9.   
  10. /** 
  11.  * 利用報(bào)表使用JExcel生成xls報(bào)表 
  12.  */  
  13. public class XlsJExcelExport extends BaseExcelExport {  
  14.   
  15.     protected JRXlsAbstractExporter getXlsExporter() {  
  16.         return new JExcelApiExporter();  
  17.     }  
  18. }    

由于在一個(gè)里面不能貼太多的代碼,故放置代碼在此處。
上接:http://jimmy-shine.javaeye.com/blog/123595

PDF格式的:

java 代碼
 
  1. /** 
  2.  * @copyRight Beijing Tsing-Tech Reachway Software Co.,Ltd. 
  3.  * @author Jimmy.Shine 2007-5-12 
  4.  */  
  5. package cn.com.reachway.framework.report.export;  
  6.   
  7. import java.io.IOException;  
  8. import java.io.OutputStream;  
  9. import java.net.URLEncoder;  
  10. import java.sql.Connection;  
  11. import java.util.List;  
  12. import java.util.Map;  
  13.   
  14. import javax.servlet.http.HttpServletRequest;  
  15. import javax.servlet.http.HttpServletResponse;  
  16.   
  17. import net.sf.jasperreports.engine.JRDataSource;  
  18. import net.sf.jasperreports.engine.JRException;  
  19. import net.sf.jasperreports.engine.JRExporterParameter;  
  20. import net.sf.jasperreports.engine.JasperPrint;  
  21. import net.sf.jasperreports.engine.export.JRPdfExporter;  
  22. import net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;  
  23. import cn.com.reachway.framework.exception.JasperReportException;  
  24. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithConnection;  
  25. import cn.com.reachway.framework.report.jasperPrint.JasperPrintWithDataSource;  
  26.   
  27. /** 
  28.  *  
  29.  */  
  30. public class PDFExport {  
  31.   
  32.     /** 
  33.      * 導(dǎo)出報(bào)表 
  34.      *  
  35.      * @param request 
  36.      * @param response 
  37.      * @param reportFilePath 
  38.      * @param params 
  39.      * @param con 
  40.      * @param fileName 
  41.      * @throws JasperReportException 
  42.      */  
  43.     public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,  
  44.             Connection con, String fileName) throws JasperReportException {  
  45.   
  46.         JasperPrint jasperPrint = new JasperPrintWithConnection(reportFilePath, params, con).getJasperPrint();  
  47.         // 將填充完的japserPrint放入session中。  
  48.         request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);  
  49.         // 拿到j(luò)apserPrintList  
  50.         List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);  
  51.         // 若沒(méi)有JasperPrintList,則拋出異常  
  52.         if (jasperPrintList == null) {  
  53.             throw new JasperReportException("在Http Session中沒(méi)有找到JasperPrint List");  
  54.         }  
  55.         try {  
  56.             OutputStream ouputStream = response.getOutputStream();  
  57.             try {  
  58.                 response.setContentType("application/pdf");  
  59.                 response.setCharacterEncoding("UTF-8");  
  60.                 if (fileName == null || fileName.equals(""))  
  61.                     response.setHeader("Content-Disposition""inline; filename=\"noTitle.pdf\"");  
  62.                 else  
  63.                     response.setHeader("Content-Disposition""inline; filename=\""  
  64.                             + URLEncoder.encode(fileName, "UTF-8") + ".pdf\"");  
  65.                 // 使用JRPdfExproter導(dǎo)出器導(dǎo)出pdf  
  66.                 JRPdfExporter exporter = new JRPdfExporter();  
  67.                 // 設(shè)置JasperPrintList  
  68.                 exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);  
  69.   
  70.                 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);  
  71.                 exporter.exportReport();  
  72.             } catch (JRException e) {  
  73.                 e.printStackTrace();  
  74.                 throw new JasperReportException("在導(dǎo)出pdf格式報(bào)表時(shí)發(fā)生錯(cuò)誤");  
  75.             } finally {  
  76.                 if (ouputStream != null) {  
  77.                     try {  
  78.                         ouputStream.close();  
  79.                     } catch (IOException ex) {  
  80.                     }  
  81.                 }  
  82.             }  
  83.         } catch (IOException ioe) {  
  84.             ioe.printStackTrace();  
  85.             throw new JasperReportException("從Response中取得OutputStream時(shí)發(fā)生錯(cuò)誤!");  
  86.         }  
  87.     }  
  88.   
  89.     /** 
  90.      * 導(dǎo)出報(bào)表 
  91.      *  
  92.      * @param request 
  93.      * @param response 
  94.      * @param reportFilePath 
  95.      * @param params 
  96.      * @param dataSource 
  97.      * @param fileName 
  98.      * @throws JasperReportException 
  99.      */  
  100.     public void export(HttpServletRequest request, HttpServletResponse response, String reportFilePath, Map params,  
  101.             JRDataSource dataSource, String fileName) throws JasperReportException {  
  102.   
  103.         JasperPrint jasperPrint = new JasperPrintWithDataSource(reportFilePath, params, dataSource).getJasperPrint();  
  104.         // 將填充完的japserPrint放入session中。  
  105.         request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);  
  106.         // 拿到j(luò)apserPrintList  
  107.         List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);  
  108.         // 若沒(méi)有JasperPrintList,則拋出異常  
  109.         if (jasperPrintList == null) {  
  110.             throw new JasperReportException("在Http Session中沒(méi)有找到JasperPrint List");  
  111.         }  
  112.         try {  
  113.             OutputStream ouputStream = response.getOutputStream();  
  114.             try {  
  115.                 response.setContentType("application/pdf");  
  116.                 response.setCharacterEncoding("UTF-8");  
  117.                 if (fileName == null || fileName.equals(""))  
  118.                     response.setHeader("Content-Disposition""inline; filename=\"noTitle.pdf\"");  
  119.                 else  
  120.                     response.setHeader("Content-Disposition""inline; filename=\""  
  121.                             + URLEncoder.encode(fileName, "UTF-8") + ".pdf\"");  
  122.                 // 使用JRPdfExproter導(dǎo)出器導(dǎo)出pdf  
  123.                 JRPdfExporter exporter = new JRPdfExporter();  
  124.                 // 設(shè)置JasperPrintList  
  125.                 exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);  
  126.   
  127.                 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);  
  128.                 exporter.exportReport();  
  129.             } catch (JRException e) {  
  130.                 e.printStackTrace();  
  131.                 throw new JasperReportException("在導(dǎo)出pdf格式報(bào)表時(shí)發(fā)生錯(cuò)誤");  
  132.             } finally {  
  133.                 if (ouputStream != null) {  
  134.                     try {  
  135.                         ouputStream.close();  
  136.                     } catch (IOException ex) {  
  137.                     }  
  138.                 }  
  139.             }  
  140.         } catch (IOException ioe) {  
  141.             ioe.printStackTrace();  
  142.             throw new JasperReportException("從Response中取得OutputStream時(shí)發(fā)生錯(cuò)誤!");  
  143.         }  
  144.     }  
  145.   
  146. }  
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
JasperReport報(bào)表設(shè)計(jì)總結(jié)(二)
利用Ireport和JasperReport實(shí)現(xiàn)導(dǎo)出RTF文件
圖示ireport中使用javabean作數(shù)據(jù)源開(kāi)發(fā)基于jasperreports報(bào)表過(guò)程
一次 applet 客戶(hù)端打印 jasperreport 報(bào)表任務(wù)筆記
iReport+jasperReports制作WEB報(bào)表
ireport導(dǎo)出各種格式(pdf,excel,word,html,print)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服