JAVA调用BIRT设置的SCRIPT数据集

来源:互联网 发布:网络虚拟号发短信软件 编辑:程序博客网 时间:2024/05/01 14:33

第一步:定义一下JAVA,class(看后面附件)
第二步:定义一个LIST
第三步:在BIRT报表的DATASET事件中(open)中输入
   count = 0;
   cf = new Packages.user.ContactListFactory();
   c = cf.createContactList();
在BIRT报表的DATASET事件中(CLOSE)中输入
   cf=null; c=null;
在BIRT报表的DATASET事件中(FETCH)中输入
  if (count <= c.length-1){
   row["columnFirstName"] = c[count].getFname();
   row["columnLastName"] = c[count].getLname();
   row["columnPhoneNumber"]= c[count].getPhone();
   count ++; return true; }
   return false;
   -

###############################################

                                               附件

################################################

  第一二步的两个JAVA类代码发下:
   A:package user;
    public class Contact {
      String fname;
      String lname;
      String phone;
      public Contact(String fname, String lname, String phone){
        this.fname = fname;
        this.lname = lname;
        this.phone = phone;
      }
      /*
      *
      * @return Returns the fname.
      */
      public String getFname() {
       return fname;
      }
      /**
      * @param fname The fname to set.
      */
      public void setFname(String fname) {
        this.fname = fname;
      }
      /**
      * @return Returns the lname.
      */
       public String getLname() {
         return lname;
       }
       /**
       * @param lname The lname to set.
       */
       public void setLname(String lname) {
          this.lname = lname;
       }
       /**
       * @return Returns the phone.
       */
       public String getPhone() {
         return phone;
       }
       /**
       * @param phone The phone to set.
       */
       public void setPhone(String phone) {
        this.phone = phone;
       }
     }
    
    
     B:
     package user;
     public class ContactListFactory {
       public Contact[] createContactList(){
         Contact[] c = new Contact[5];
         c[0] = new Contact("stavros", "kounis", "2310886269");
         c[1] = new Contact("dimitris", "kounis", "2310888270");
         c[2] = new Contact("dimitris", "adamos", "2310998417");
         c[3] = new Contact("nikos", "koufotolis", "2321013770");
         c[4] = new Contact("yan", "liang", "13824745919");
         return c;
        }
      }
     
      注:发布后将两个JAVA类产生的CLASS 放在
          C:/eclipse/plugins/org.eclipse.birt.report.viewer_2.1.0.N20060628-1351/birt/WEB-INF/classes/user 目录下,
          USER是开发时的包
         
          经过发上步骤,再写一个JAVA PROJECT
          调用上述报表代码如下
         
          package user;
          import javax.swing.JOptionPane;
          import user.ExecuteReport;
          public class Reporttest {
           public static void main(String args[]) {
            try {
             ExecuteReport mreport = new ExecuteReport();
             mreport.executeReport();
            }catch ( Exception e ) {
             e.printStackTrace();
            }
           /*System.out.println("中国人民解");
           JOptionPane.showMessageDialog(null,"中国人民解");
           System.exit(0);
           */
          }
         }
        
        
         主代码JAVA:
        
          package user;
          import java.util.Collection;
          import java.util.HashMap;
          import java.util.logging.Level;
          import javax.swing.text.html.HTMLDocument.Iterator;
          import org.eclipse.birt.core.framework.Platform;
          import org.eclipse.birt.report.engine.api.EngineConfig;
          import org.eclipse.birt.report.engine.api.EngineConstants;
          import org.eclipse.birt.report.engine.api.EngineException;
          import org.eclipse.birt.report.engine.api.HTMLActionHandler;
          import org.eclipse.birt.report.engine.api.HTMLEmitterConfig;
          import org.eclipse.birt.report.engine.api.HTMLRenderContext;
          import org.eclipse.birt.report.engine.api.HTMLRenderOption;
          import org.eclipse.birt.report.engine.api.HTMLServerImageHandler;
          import org.eclipse.birt.report.engine.api.IGetParameterDefinitionTask;
          import org.eclipse.birt.report.engine.api.IParameterDefnBase;
          import org.eclipse.birt.report.engine.api.IParameterGroupDefn;
          import org.eclipse.birt.report.engine.api.IParameterSelectionChoice;
          import org.eclipse.birt.report.engine.api.IReportEngine;
          import org.eclipse.birt.report.engine.api.IReportEngineFactory;
          import org.eclipse.birt.report.engine.api.IReportRunnable;
          import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
          import org.eclipse.birt.report.engine.api.IScalarParameterDefn;
          public class ExecuteReport {
           static void executeReport() throws EngineException{
            IReportEngine engine=null;
            EngineConfig config = null;
            try{
             //Configure the Engine and start the Platform
              config = new EngineConfig( );
              config.setEngineHome( "C:/eclipse/workspace/ReportDrive/birt-runtime-2_1_0/ReportEngine" );
              config.setLogConfig(null, Level.FINE);
              Platform.startup( config );
              IReportEngineFactory factory = (IReportEngineFactory) Platform .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
              engine = factory.createReportEngine( config );
              engine.changeLogLevel( Level.WARNING );
            }catch( Exception ex){
             ex.printStackTrace();
            }
            IReportRunnable design = null;
            //Open the report design
            design = engine.openReportDesign("C:/eclipse/workspace/html/kenScripDataset.rptdesign");
            IRunAndRenderTask task = engine.createRunAndRenderTask(design);
            //Set rendering options - such as file or stream output,
            //output format, whether it is embeddable, etc
            HTMLRenderOption options = new HTMLRenderOption();
            /**************************************************/

            HashMap parameters = new HashMap();
            String name = "myreportname";
            String pvalue ="客户产品工序表";
            parameters.put(name,pvalue);
            task.setParameterValues(parameters);
            task.validateParameters(); 
            /**************************************************/

            options.setOutputFileName("C:/eclipse/workspace/html/kenScripDataset.pdf");
            //Set output format options.setOutputFormat("pdf");
            task.setRenderOption(options);
            //run the report and destroy the engine
            //Note - If the program stays resident do not shutdown the Platform or the Engine
            task.run();
            task.close();
            engine.shutdown();
            Platform.shutdown();
            System.out.println("Finished");
            System.exit(0);
           }
          }

          ----------------------------------------
          在eclispe 3.2的PATH BUILD中加入
          BIRT 的com.ibm.icu_3.4.4.1 .JAR BIRT
          的engineapi.JAR BIRT 的coreapi.JAR BIRT 的JS.JAR