JAVA 호출 BIRT 설정 SCRIPT 데이터 세트

11242 단어 자바
배열 방식:
STEP 1:JAVA,class 를 정의 합 니 다.
STEP 2:LIST 정의
세 번 째 단계:BIRT 보고서 의 DATASET 이벤트(open)에 입력
  
count = 0;
   cf = new Packages.user.ContactListFactory();
   c = cf.createContactList(); 

BIRT 보고서 의 DATASET 이벤트(CLOSE)에 입력
  
BIRT 보고서 의 DATASET 이벤트(FETCH)에 입력
 
cf=null; c=null; 

  
목록 방식
STEP 1:JAVA,class 를 정의 합 니 다.
STEP 2:LIST 정의
세 번 째 단계:BIRT 보고서 의 DATASET 이벤트(open)에 입력
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;

BIRT 보고서 의 DATASET 이벤트(CLOSE)에 입력

importPackage(Packages.com.jshx);
reslut=reslutList;
count=0;

BIRT 보고서 의 DATASET 이벤트(FETCH)에 입력
reslut=null; 

###############################################
                                               첨부 파일
################################################
연락처 클래스

if (count<=reslut.size()){
   row["columnFirstName"] = reslut.get(count).getFname();
   row["columnLastName"] = reslut.get(count).getLname();
   row["columnPhoneNumber"]= reslut.get(count).getPhone();
   count++; 
   return true; 
}
return false;

ContactListFactory 클래스

package com.jshx;

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; 
      }
}

공공 방법 류
CommonFunc.java

package com.jshx;

import java.util.ArrayList;
import java.util.List;

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; 
       }
	public List<Contact> createList(){
		List<Contact> c = new ArrayList<Contact>(); 
        c.add(new Contact("stavros", "kounis", "2310886269")); 
        c.add(new Contact("dimitris", "kounis", "2310888270")); 
        c.add(new Contact("dimitris", "adamos", "2310998417")); 
        c.add(new Contact("nikos", "koufotolis", "2321013770")); 
        c.add(new Contact("yan", "liang", "13824745919")); 
        return c; 
       }
}

Constants.java

package com.jshx.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class CommonFunc {
	private static final Log logger = LogFactory.getLog(CommonFunc.class);
	/**
	 *              
	 * @param classesPath
	 * @return
	 */
    public static String getPorjectPath(String classesPath){   
      String tempdir;
      String classPath[] = classesPath.split("webapps");
      tempdir=classPath[0];
      if(!"/".equals(tempdir.substring(tempdir.length()-1))){
    	  tempdir += "/";
      }
      return tempdir;   
    }
	
	 /**
     *        
     * @param date
     * @param formatStr
     * @return
     */
    public static String ConvertDateToStr(Date date,String formatStr)
	{
		try {
			SimpleDateFormat format = new SimpleDateFormat(formatStr);
			return format.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}


ExecuteReport 클래스

package com.jshx.util;

/**
 *     <      >
 * @author duanpf
 * @version [   , 2012-12-8]
 * @see [   /  ]
 * @since [  /    ]
 */

public class Constants{
	// Field descriptor #6 Ljava/lang/String;
	public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
	  
	// Field descriptor #6 Ljava/lang/String;
	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	  
	// Field descriptor #6 Ljava/lang/String;
	public static final String YYYY_MM_DD = "yyyy-MM-dd";
	
	// Field descriptor #6 Ljava/lang/String;
	public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
	
	public static final String YYYYMM = "yyyyMM";
	
	public static final String YYYY = "yyyy";
	
	public static final String MM = "MM";
	
	public static final String birtRunTime = "virtualdir/birt/runtime/ReportEngine";
	
	public static final String birtPath = "virtualdir/birt/report/";
}

Reporttest 클래스

package com.jshx;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
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.HTMLRenderContext;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.ReportEngine;

import com.jshx.util.CommonFunc;
import com.jshx.util.Constants;

public class ExecuteReport {
public static Logger logger = Logger.getLogger(ExecuteReport.class);
 
 /**
	 * 
	 * @param paraMap             , LIST  
	 * @param rptdesign	    
	 * @param outFlie	      
	 * @param outType	       
	 * @return
	 * @throws EngineException
	 */
 public String executeReport(Map<String, Object> paraMap,String rptdesign,String outFlie,String outType) throws EngineException{
 	String classesPath = this.getClass().getClassLoader().getResource("").getPath();
 	String realPath= CommonFunc.getPorjectPath(classesPath);
 	
 	//Engine Configuration - set and get temp dir, BIRT home, Servlet context
 	EngineConfig config = new EngineConfig();
 	logger.info("     ="+realPath+ Constants.birtRunTime);
 	config.setEngineHome(realPath+ Constants.birtRunTime); 

 	//Create the report engine
 	ReportEngine engine = new ReportEngine(config);
 	
 	//Open a report design - use design to modify design, retrieve embedded images etc.
 	IReportRunnable design = engine.openReportDesign(realPath+ Constants.birtPath +rptdesign);
 	
 	//Create task to run the report - use the task to execute and run the report,
 	IRunAndRenderTask task = engine.createRunAndRenderTask(design);
 	
 	//Set Render context to handle url and image locataions
		HTMLRenderContext renderContext = new HTMLRenderContext();
		renderContext.setImageDirectory("image");
		HashMap contextMap = new HashMap();
		contextMap.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext);
		task.setAppContext( contextMap );
		  
		if(null!=paraMap&&!paraMap.isEmpty()){
			Set<String> set = paraMap.keySet();
			for (String string : set) {
				if("reslut".equals(string)){
					Contact[] c =(Contact[]) paraMap.get(string);
					task.addScriptableJavaObject("reslut", c);
				}if("urlPath".equals(string)){
					task.addScriptableJavaObject(string, paraMap.get("urlPath")); 
				}else{
					//List<Contact> list = (ArrayList<Contact>)paraMap.get(string);
                     List list =(ArrayList) paraMap.get(string);
					task.addScriptableJavaObject(string, list);
				}
			}
		}
		
		//Set rendering options - such as file or stream output, 
		  //output format, whether it is embeddable, etc
		HTMLRenderOption options = new HTMLRenderOption();
		Date dateNow=new Date();
		String reqTime = CommonFunc.ConvertDateToStr(new Date(), Constants.YYYYMM);
		String dateNowStr= reqTime +"/"+outFlie;
		String birtFile = realPath+ Constants.birtPath+dateNowStr;
		options.setOutputFileName(birtFile);
		options.setOutputFormat(outType);
		task.setRenderOption(options);
		
		//run the report and destroy the engine
		try{
			task.run();
			engine.destroy();
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return dateNowStr;
 }
 public static void main(String[] args) throws Exception {
 	Map<String, Object> reslutMap = new HashMap<String, Object>();
 	ExecuteReport pf = new ExecuteReport();
 	pf.executeReport(reslutMap,"scripted.rptdesign","scripted.xls","xls");
}
} 

좋은 웹페이지 즐겨찾기