您的位置:首页 > 编程语言 > Java开发

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

2013-05-09 11:53 555 查看
整个实现流程是这样的:

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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐