使用BO JAVA SDK打开WEBI报表,并进行PDF、EXCEL、CSV、HTML导出

整个实现流程是这样的:

1、打开一个WEBI报表

2、运行查询

3、设置查询条件

4、进行导出

5、关闭资源

需要注意的是,在设置查询条件之前,需要运行一次查询。

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;

import com.businessobjects.rebean.wi.BinaryView;
import com.businessobjects.rebean.wi.CSVView;
import com.businessobjects.rebean.wi.DocumentInstance;
import com.businessobjects.rebean.wi.HTMLView;
import com.businessobjects.rebean.wi.OutputFormatType;
import com.businessobjects.rebean.wi.PaginationMode;
import com.businessobjects.rebean.wi.Prompt;
import com.businessobjects.rebean.wi.Prompts;
import com.businessobjects.rebean.wi.Report;
import com.businessobjects.rebean.wi.ReportEngine;
import com.businessobjects.rebean.wi.ReportEngines;
import com.businessobjects.rebean.wi.Reports;
import com.crystaldecisions.sdk.exception.SDKException;
import com.crystaldecisions.sdk.framework.CrystalEnterprise;
import com.crystaldecisions.sdk.framework.IEnterpriseSession;
import com.crystaldecisions.sdk.framework.ISessionMgr;
import com.crystaldecisions.sdk.occa.infostore.IInfoObject;
import com.crystaldecisions.sdk.occa.infostore.IInfoObjects;
import com.crystaldecisions.sdk.occa.infostore.IInfoStore;

public class ExportWebiDocument {

	public static void main(String[] args) {
		// The exported documents will be saved to this folder
		System.out.println("Working Directory: " + System.getProperty("user.dir"));
	
		IEnterpriseSession enterpriseSession = null;
		ReportEngines reportEngines = null;
		try {
			
			String host = "localhost";
			String user = "Administrator";
			String pass = "";
			String auth = "secEnterprise";
			String name = "/Report Samples/My Report";
			
			// Prepare answers to prompts 
			// It is assumed that the report has two prompts "Country:" and "Year:"
			HashMap<String, String[]> answers = new HashMap<String, String[]>();
			answers.put("Country:", new String[]{"US", "France"});
			answers.put("Year:", new String[]{"FY2004"});
			
			// Connect to CMS
			System.out.println("Connecting...");
			ISessionMgr sessionMgr = CrystalEnterprise.getSessionMgr();
			enterpriseSession = sessionMgr.logon(user, pass, host, auth);
			
			// Initialize Webi report engine
			reportEngines = (ReportEngines) enterpriseSession.getService("ReportEngines");
			ReportEngine reportEngine = (ReportEngine) reportEngines.getService(ReportEngines.ReportEngineType.WI_REPORT_ENGINE);
			
			// Retrive the list of all document and search the one with the specified name
			// If the Id or CUID of the document is known, the query can be more specific,
			// so there will be no need to loop through whole list of Webi documents.
			IInfoStore infoStore = (IInfoStore) enterpriseSession.getService("InfoStore");
			String query = "select SI_ID, SI_NAME, SI_FILES from CI_INFOOBJECTS where SI_KIND = 'Webi' and SI_INSTANCE=0";
			IInfoObjects infoObjects = (IInfoObjects) infoStore.query(query);
			for (Object object : infoObjects) {
				IInfoObject infoObject = (IInfoObject) object;
				String path = getInfoObjectPath(infoObject);
				System.out.println(path);
				if (path.equals(name)) {
					
					String title = infoObject.getTitle();
					
					// Open the document
					DocumentInstance doc = reportEngine.openDocument(infoObject.getID());
					
					// Refresh the document
					doc.refresh();
					
					// Set prompts
					Prompts prompts = doc.getPrompts();
					for (int i = 0; i < prompts.getCount(); i++) {
						Prompt prompt = prompts.getItem(i);
						System.out.println(prompt.getID());
						String[] answer = answers.get(prompt.getID());
						if (answer != null) {							
							prompt.enterValues(answer);
							for (String value : answer) {
								System.out.println("  " + value);
							}
						}
					}
					doc.setPrompts();
					
					// Check that all mandatory prompts are answered
					if (doc.getMustFillPrompts()) {
						System.out.println("ERROR: Mandatory prompts has not been entered");
					}
					
					// Check the contexts do not need to be resolved
					if (doc.getMustFillContexts()) {
						System.out.println("ERROR: Contexts has not been entered");
					}
					
					// CSV
					CSVView csvView = (CSVView)doc.getDataProviders().getView(OutputFormatType.CSV);
					writeBytes(csvView.getContent().getBytes(), title + " " + ".csv");
					
					// PDF
					BinaryView binaryView2 = (BinaryView)doc.getView(OutputFormatType.PDF); 
					writeBytes(binaryView2.getContent(), title + ".pdf");
					
					// XLS
					BinaryView xlsView = (BinaryView)doc.getView(OutputFormatType.XLS);
					writeBytes(xlsView.getContent(), title + ".xls");

					Reports reports = doc.getReports();
					for (int i = 0; i < reports.getCount(); i++)
					{
						Report report = reports.getItem(i);
						report.setPaginationMode(PaginationMode.Listing);
					
						// HTML
						HTMLView htmlView = (HTMLView) report.getView(OutputFormatType.DHTML);
						writeBytes(htmlView.getContent().getBytes(), title + " " + i + ".html");
					}
					
					doc.closeDocument();					
				}
			}

		} catch (SDKException ex) {
			ex.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reportEngines != null)
				reportEngines.close();
			if (enterpriseSession != null)
				enterpriseSession.logoff();
			System.out.println("Finished!");
		}		
	}

	public static String getInfoObjectPath(IInfoObject infoObject) throws SDKException {
		String path = "/" + infoObject.getTitle();
		while (infoObject.getParentID() != 0) {
			infoObject = infoObject.getParent();
			path = "/" + infoObject.getTitle() + path;
		}
		return path;
	}
	
	public static void writeBytes(byte[] data, String filename) throws IOException {
		File file = new File(filename); 
		FileOutputStream fstream = new FileOutputStream(file); 
		fstream.write(data); 
		fstream.close();
	}
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值