var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-20738293-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script')"/>

          jutleo
          歡迎走進有風的地方~~
          posts - 63,  comments - 279,  trackbacks - 0
           JasperFillManager.fillReport()這個方法在使用JDBC數據源時采用一個打開的數據庫連接(getConn),除此之外jasperReport給我們提供了一個JRDataSource接口,用以實現我們自己的數據源
          JRDataSource接口只有兩個方法
          public interface JRDataSource
          {
             
          /**
               * 針對當前的數據源返回游標指向的下一個
          元素的值,
               *

               
          */
              
          public boolean next() throws JRException;
            
          /**
               * 返回游標指向的當前值
               *

               
          */
              
          public Object getFieldValue(JRField jrField) throws JRException;
          }
          JRBeanCollectionDataSource
          此種方式是最簡單的一種,查看API我們就可以發現,JRBeanCollectionDataSource繼承JRAbstractBeanDataSource類,而JRAbstractBeanDataSource是一個抽象類它間接的實現了JRDataSource這個接口,所以我們就可以不用自己去實現next()/getFieldValue()這兩個方法了。
                  看到JRBeanCollectionDataSource這個類名大概就知道怎么用了吧!
                  我們的模板文件還是上篇的JDBC數據源的模板,只是沒有了查詢語句,手工建立所需的幾個字段(域)
          新建一vo對象Person.java,最基本的getter、setter方法
          package org.bulktree.ireport.customdata;

          /**
           * custom data
           * 
           * 
          @author bulktree Email: laoshulin@gmail.com @ Nov 7, 2008
           
          */

          public class Person {
              
          private String pid;
              
          private String name;
              
          private String sex;
              
          private String age;
              
          private String password;
              
          private String department;

              
          public Person(String pid, String name, String sex, String age, String password,
                      String department) 
          {
                  
          this.pid = pid;
                  
          this.name = name;
                  
          this.sex = sex;
                  
          this.age = age;
                  
          this.password = password;
                  
          this.department = department;
              }


              
          public Person() {

              }


              
          }


          下來準備數據
          Person p1 = new Person();
                  p1.setAge(
          "23");
                  p1.setDepartment(
          "ISoftStone");
                  p1.setName(
          "LAOSHULIN");
                  p1.setPassword(
          "123456789");
                  p1.setPid(
          "2008040516058772hj");
                  p1.setSex(
          "man");
          既然它是一個BeanCollection我們 就來一個從conllection吧!
          List<Person> list = new ArrayList<Person>();
                  list.add(p1);
          查看API,JRBeanCollectionDataSource的構造函數
          public JRBeanCollectionDataSource(Collection beanCollection)
              
          {
                  
          this(beanCollection, true);
              }

              
          /**
               *
               
          */

              
          public JRBeanCollectionDataSource(Collection beanCollection, boolean isUseFieldDescription)
              
          {
                  
          super(isUseFieldDescription);
                  
                  
          this.data = beanCollection;

                  
          if (this.data != null)
                  
          {
                      
          this.iterator = this.data.iterator();
                  }

              }
          看看描述我們使用第一個
          JRDataSource datesource = new JRBeanCollectionDataSource(list);
          這個數據源就構造完畢了,很簡單吧!這樣報表就可以預覽了,完整的代碼如下:
          package org.bulktree.ireport.customdata;

          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileNotFoundException;
          import java.io.InputStream;
          import java.sql.Connection;
          import java.sql.DriverManager;
          import java.sql.ResultSet;
          import java.sql.Statement;
          import java.util.ArrayList;
          import java.util.HashMap;
          import java.util.List;
          import java.util.Map;

          import net.sf.jasperreports.engine.JRDataSource;
          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.data.JRBeanCollectionDataSource;
          import net.sf.jasperreports.engine.util.JRLoader;
          import net.sf.jasperreports.view.JRViewer;
          import net.sf.jasperreports.view.JasperViewer;

          public class JasperReportDemo {
              
          public static void main(String[] args) throws Exception {
                  JasperReportDemo jrd 
          = new JasperReportDemo();
                  jrd.reportMethod(jrd.getMap());
              }


              
          public void reportMethod(Map map) throws Exception {
                  JasperReport jasperReport 
          = null;
                  JasperPrint jasperPrint 
          = null;

                  
          try {
                      
          /*
                       * File file = new File("Person.jrxml"); InputStream in = new
                       * FileInputStream(file); // 編譯報表 jasperReport =
                       * JasperCompileManager.compileReport(in);
                       
          */

                      
          // 實際中編譯報表很耗時,采用Ireport編譯好的報表
                      jasperReport = (JasperReport) JRLoader
                              .loadObject(
          "D:\\workspace\\Person.jasper");
                      
          // 填充報表
                      jasperPrint = JasperFillManager
                              .fillReport(jasperReport, map, getDataSource());
                      
          // JasperExportManager.exportReportToHtmlFile(jasperPrint,
                      
          // "test.html");
                      JasperViewer jasperViewer = new JasperViewer(jasperPrint);
                      jasperViewer.setVisible(
          true);
                  }
           catch (JRException e) {
                      e.printStackTrace();
                  }
           catch (FileNotFoundException e) {
                      e.printStackTrace();
                  }

              }


              
          public Map getMap() {

                  Map map 
          = new HashMap();
                  map.put(
          "reportTitle""laoshulin");
                  
          return map;
              }


              
          public JRDataSource getDataSource() throws Exception {

                  
          // 自定義數據源
                  Person p1 = new Person();
                  p1.setAge(
          "23");
                  p1.setDepartment(
          "ISoftStone");
                  p1.setName(
          "LAOSHULIN");
                  p1.setPassword(
          "123456789");
                  p1.setPid(
          "2008040516058772hj");
                  p1.setSex(
          "man");
                  List
          <Person> list = new ArrayList<Person>();
                  list.add(p1);

                  JRDataSource datesource 
          = new JRBeanCollectionDataSource(list);

                  
          return datesource;
              }

          }

          看看效果吧!
          簡簡單單的幾行代碼就可以完成一個簡單的報表,我們看到的這些工具欄圖表還有窗口的一些標題啊等等都可以自己的定制哦,下來慢慢介紹!
          posted on 2008-12-12 09:56 凌晨風 閱讀(6338) 評論(10)  編輯  收藏 所屬分類: iReport + JasperReport 系列

          FeedBack:
          # re: iReport+jasperReport之BEAN數據源
          2008-12-12 11:38 | 楊愛友
          如果某列顯示的是公司地址,有的公司地址“變態”,多大200個漢字,有的只有十幾個,那你能動態設定這一列的寬度和高度嗎?
          以前我沒有做到,只能設定足夠的寬度和高度來顯示這個地址。  回復  更多評論
            
          # re: iReport+jasperReport之BEAN數據源
          2008-12-12 12:55 | 凌晨風
          哈哈 這個問題是很多報表都會出現的問題,那要看你的紙張了,要是紙張不限制的話你可以設置盡可能的大些防止撐開影響美觀,要是想動態改變也不是很難,jasperReport可以動態的改變你所涉及的任何域的所有屬性,所以你說的那個就很容易實現,這些東西我都有測試過,之后有空會都寫出來的  回復  更多評論
            
          # re: iReport+jasperReport之BEAN數據源
          2009-01-06 16:05 | zsyzk@qq.com
          JasperViewer jasperViewer = new JasperViewer(jasperPrint);
          jasperViewer.setVisible(true);

          直接這樣打開預覽,關閉后,會把WEB服務都關掉呢,怎么解決這個問題???  回復  更多評論
            
          # re: iReport+jasperReport之BEAN數據源
          2009-01-07 09:03 | 凌晨風
          怎么會關閉web服務呢?那你要檢查一下你的代碼了,建議你實現自己的JasperViewer  回復  更多評論
            
          # re: iReport+jasperReport之BEAN數據源[未登錄]
          2009-03-27 17:40 | 西西

          這樣寫就不會關掉了
          JasperViewer jasperViewer = new JasperViewer(jasperPrint,false);   回復  更多評論
            
          # re: iReport+jasperReport之BEAN數據源
          2009-08-26 11:03 | 痞子

          JasperViewer jrview=new JasperViewer(jasperPrint,false);
          就不會關閉服務了

            回復  更多評論
            
          # re: iReport+jasperReport之BEAN數據源[未登錄]
          2009-09-02 11:30 | footprint
          博主能不能提供一下XML文件呀?DETAIL域總是空白  回復  更多評論
            
          # re: iReport+jasperReport之BEAN數據源
          2014-04-19 10:02 | rober
          @footprint
          就是,LZ XML配置給一個吧,不知道怎么配置啊~~~  回復  更多評論
            
          # re: iReport+jasperReport之BEAN數據源
          2014-05-15 14:09 | 周研
          List<OauthApi> list = reportService.getApiList();
          JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(
          list);

          當list為空時,生成的pdf或者excel文件不會報錯,但是沒有了title和Column Header了  回復  更多評論
            
          # re: iReport+jasperReport之BEAN數據源[未登錄]
          2014-11-12 17:17 | hh
          @凌晨風
          瞎說,是可以遍歷所有燃素,但不能改變屬性,因為一旦模板本件確定,就無法改,只能改變其中的值  回復  更多評論
            

          <2014年11月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          常用鏈接

          留言簿(11)

          我參與的團隊

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          新聞分類

          新聞檔案

          收藏夾

          圍脖

          最新隨筆

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 仲巴县| 响水县| 武城县| 定陶县| 昌乐县| 白玉县| 子长县| 攀枝花市| 武强县| 满城县| 仙游县| 太湖县| 鱼台县| 安溪县| 房产| 吴川市| 佳木斯市| 乌兰浩特市| 宝兴县| 文安县| 中山市| 科技| 岳阳市| 财经| 昌平区| 安庆市| 龙南县| 扶沟县| 晴隆县| 梓潼县| 和田市| 黎川县| 稻城县| 富民县| 黔西县| 丰都县| 项城市| 宁波市| 丰台区| 武威市| 兴国县|