使用BO JAVA SDK打开WEBI报表,并进行PDF、EXCEL、CSV、HTML导出
2013-05-09 11:53
555 查看
整个实现流程是这样的:
1、打开一个WEBI报表
2、运行查询
3、设置查询条件
4、进行导出
5、关闭资源
需要注意的是,在设置查询条件之前,需要运行一次查询。
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(); } }
相关文章推荐
- 使用jasperreports制作报表(导出pdf excel html)
- Java程序员从笨鸟到菜鸟之(一百零六)java操作office和pdf文件(四)页面列表导出cvs,excel、pdf报表.
- java导出csv用excel打开后数字不用科学计数法显示
- java操作office和pdf文件(四)页面列表导出cvs,excel、pdf报表.
- java操作office和pdf文件页面列表导出cvs,excel、pdf报表.
- Java程序员从笨鸟到菜鸟之(一百零四)java操作office和pdf文件(二)利用POI实现数据导出excel报表
- DataTable导出为word,excel,html,csv,pdf,.txt
- c#使用输出html字符串进行导出excel功能时,常用到得几种格式
- Java程序员从笨鸟到菜鸟之(一百零四)java操作office和pdf文件(二)利用POI实现数据导出excel报表
- ios 使用NSURlSession 下载文件并且使用进度条进行文件下载进度读取以及打开附件(word,excel,ppt,pdf)显示
- 使用Java将PDF解析成HTML页面进行展示并从页面中提取Json数据设置到Table中
- asp.net导出excel-一行代码实现excel、xml、pdf、word、html、csv等7种格式文件导出功能而且美观-SNF快速开发平台
- jasperreport实现Html、Pdf、Rtf、 Excel、Xml报表导出
- java使用poi将html导出word,默认打开页面视图
- java操作office和pdf文件页面列表导出cvs,excel、pdf报表.
- java导出csv用excel打开后数字不用科学计数法显示
- java中使用JXL对Excel文件进行数据的写入、导出操作
- JAVA中JSP应用导出Excel报表的简单实现以及中文乱码彻底解决(HTML)
- Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,html 格式
- [置顶] 使用Apache POI 进行Excel报表的导出下载(GET 请求 && POST请求)