版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
BIRT使用xml动态数据源总结XML数据结构EclipseWebworkMySQL因为系统需要,添加报表功能,瞄上了birt,学习之,入门教程看了一大堆,还好,多种数据源支持,对于sql还没精通,看着一大堆sql还有点犯难,所以,xml数据源成了我的首选,仔细研究之下,发现原来 birt 提供的初级功能原来很少,报表经常需要替换数据源来显示其不同的内容,而传统的设置 xml数据源只是事前指定好 xml文件,或url,google了一下,基本没有发现可用 的例子,郁闷之,自己琢磨了一周几乎,更郁闷的是其实问题早有了答案,就载在acegi的权限控制之上一直未看到胜利的曙光下面把自己动态修改 xml数据源的成功发上来我用的是 webwork,其他用户请酌情修改相应参数这个是用来view报表的action 需要传入的参数是reban.reportName= 报表文件的文件名rbean.xmlDataUrl= 报表xmldatasourceurl ,可以是stream,或xxx.xml之类的,输入浏览器能显示 xml即可BirtReportViewer.javaJava代码3.importjava.io.ByteArrayInputStream;4.importjava.io.ByteArrayOutputStream;5.importjava.io.InputStream;6.7.importjavax.servlet.ServletContext;8.importjavax.servlet.http.HttpServletRequest;9.10.importorg.eclipse.birt.report.engine.api.EngineConstants;11.importorg.eclipse.birt.report.engine.api.HTMLRenderOption;12.importorg.eclipse.birt.report.engine.api.HTMLServerImageHandler;13.importorg.eclipse.birt.report.engine.api.IHTMLRenderOption;14.importorg.eclipse.birt.report.engine.api.IRenderOption;15.importorg.eclipse.birt.report.engine.api.IReportEngine;16.import 17.import 18.import 19.import 20.import 21.import 22.import 23.import 24.import25.26.27.importcom.morepower.controller.AbstractAction;28.importcom.morepower.util.BirtEngine;29.importcom.morepower.view.BirtReporterBean;30.importcom.opensymphony.webwork.ServletActionContext;31.erceptor.ServletRequestAware;32.33.publicclassBirtReportViewerextendsAbstractActionimplements34.ServletRequestAware{35./**36.*37.*/38.privatestaticfinallongserialVersionUID=1L;39.privatestaticStringREPORT_DIR="birt-reportors";40.privatestaticStringREPORT_IMAGE_DIR="images";41.privateIReportEnginebirtReportEngine=null;42.privateHttpServletRequestrequest;43.privateBirtReporterBeanrbean;44.privateInputStreamreportStream;45.privatestaticStringDATASOURCE_NAME="DataSource";46.privatestaticStringDATASET_NAME="DataSet";47.privatestaticStringQueryText="";48.49.publicInputStreamgetReportStream(){50.returnthis.reportStream;51.}52.53.@Override54.publicStringexecute()throwsException{55.//getreportnameandlaunchtheengine56.//resp.setContentType("text/html");57.//resp.setContentType("application/pdf");58.//resp.setHeader("Content-Disposition","inline;filename=test.pdf");59.StringreportName=rbean.getReportName();60.ServletContextsc=ServletActionContext.getServletContext();61.birtReportEngine=BirtEngine.getBirtEngine(sc);62. IReportRunnabledesign=birtReportEngine.openReportDesign(sc63. ator+REPORT_DIR)64.+java.io.File.separator+reportName);65.66.ReportDesignHandlereport=(ReportDesignHandle)design67..getDesignHandle();68.69.//只是动态更换数据源即可,不需改变报表的其他结构70.//暂时不支持动态创建表格71.72.//createtasktorunandrenderreport73.buildReport(report);74.IRunAndRenderTasktask=birtReportEngine75. .createRunAndRenderTask(design);76. task.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY,77. 78.79. // set output options80. HTMLRenderOptionoptions = new HTMLRenderOption();81. options.setImageHandler(newHTMLServerImageHandler());82. 83.+REPORT_IMAGE_DIR));84.options.setBaseImageURL(request.getContextPath()85.+java.io.File.separator+REPORT_IMAGE_DIR);86.options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);87.options.setOption(IRenderOption.HTML_PAGINATION,Boolean.TRUE);88. options.setOption(IHTMLRenderOption.MASTER_PAGE_CONTENT,newBoolean(89.true));90.91.//options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF);92.options.setOption(HTMLRenderOption.HTML_ENABLE_METADATA,Boolean.FALSE);93.ByteArrayOutputStreambout=newByteArrayOutputStream();94.//Stringrenderstr=bout.toString("utf-8");95.options.setOutputStream(bout);96.task.setRenderOption(options);97.98./*99.*另外一种方案,直接在action中构造inputStream对像,但这样100.*可能导致你的报表viewer与具体业务类耦合!101.102.reportStream);103.task.getAppContext().put(org.eclipse.datat104.Boolean.TRUE);*/105.106.//runreport107.108.task.run();109.task.close();110.reportStream=newByteArrayInputStream(bout.toByteArray());111.112.returnSUCCESS;113.}114.115./**116.*清除原有的数据源117.*/118.protectedvoidclearDataSource(ReportDesignHandledesignHandle){119.//DataSourceHandledsh=designHandle.findDataSource(DATASOURCE_NAME);120.//dsh.drop();121.intcount=designHandle.getDataSources().getCount();122.123.try{124.for(inti=0;i<count;i++)125.designHandle.getDataSources().drop(i);126.}catch(SemanticExceptione){127.log.error(e);128.e.printStackTrace();129.}130.}131.132./**133.*清除原有的数据集134.*/135.protectedvoidclearDataSet(ReportDesignHandledesignHandle){136.getQueryText(designHandle);137.intcount=designHandle.getDataSets().getCount();138.try{139.for(inti=0;i<count;i++)140.designHandle.getDataSets().drop(i);141.}catch(SemanticExceptione){142.log.error(e);143.e.printStackTrace();144.}145.}146.147./**148.*149.*/150.protectedvoidgetQueryText(ReportDesignHandledesignHandle){151.QueryText=(String)designHandle.getDataSets().get(0).getProperty(152."queryText");153.}154.155.protectedvoidbuildReport(ReportDesignHandledesignHandle){156.try{157.ElementFactorydesignFactory=designHandle.getElementFactory();158.buildDataSource(designFactory,designHandle);159.buildDataSet(designFactory,designHandle);160.TableHandletable=(TableHandle)designHandle.getBody().get(0);161.table.setDataSet(designHandle.findDataSet(DATASET_NAME));162.163.}catch(SemanticExceptione){164.log.error(e);165.e.printStackTrace();166.}167.168.}169.170.protectedvoidbuildDataSource(ElementFactorydesignFactory,171.ReportDesignHandledesignHandle)throwsSemanticException{172.clearDataSource(designHandle);173.OdaDataSourceHandledsHandle=designFactory.newOdaDataSource(174.DATASOURCE_NAME,175."org.eclipse.birt.report.data.oda.xml");176./*dsHandle.setProperty("odaDriverClass",177."com.mysql.jdbc.Driver");178.dsHandle.setProperty("odaURL","jdbc:mysql://localhost/stat");179.dsHandle.setProperty("odaUser","root");180.dsHandle.setProperty("odaPassword","");*/181.dsHandle.setProperty("FILELIST",rbean.getXmlDataUrl());182.designHandle.getDataSources().add(dsHandle);183.}184.185.protectedvoidbuildDataSet(ElementFactorydesignFactory,186.ReportDesignHandledesignHandle)throwsSemanticException{187.188.clearDataSet(designHandle);189.OdaDataSetHandledsHandle=designFactory.newOdaDataSet(DATASET_NAME,190.//"org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet");191./*OdaDataSetHandledsHandle=designFactory.newOdaDataSet(DATASET_NAME,*/192."org.eclipse.birt.report.data.oda.xml.dataSet");193.dsHandle.setPrivateDriverProperty("XML_FILE", rbean.getXmlDataUrl());194.dsHandle.setPrivateDriverProperty("MAX_ROW","-1");195.dsHandle.setQueryText(QueryText);196.dsHandle.setDataSource(DATASOURCE_NAME);197.designHandle.getDataSets().add(dsHandle);198.199.}200.201.@Override202.publicvoidsetServletRequest(HttpServletRequestarg0){203.request=arg0;204.205.}206.207.@Override208.publicvoiddestory(){209.BirtEngine.destroyBirtEngine();210.}211.212.publicBirtReporterBeangetRbean(){213.returnrbean;214.}215.216.publicvoidsetRbean(BirtReporterBeanrbean){217.this.rbean=rbean;218.}219.220.@Override221.publicvoidinitilize(){222.try{223.("报表系统初始化中");224.BirtEngine.initBirtConfig();225.REPORT_DIR=BirtEngine.getBirtReportorProperty("report_dir");226.REPORT_IMAGE_DIR=BirtEngine227..getBirtReportorProperty("report_image_dir");228.DATASOURCE_NAME="DataSource";229.DATASET_NAME="DataSet";230.}catch(Exceptione){231.e.printStackTrace();232.log.error(e);233.}234.}235.}BirtEngine.javaJava代码6.7.mons.logging.Log;8.mons.logging.LogFactory;9.importorg.eclipse.birt.report.engine.api.EngineConfig;10.importorg.eclipse.birt.report.engine.api.IReportEngine;11.importjavax.servlet.*;12.importorg.eclipse.birt.core.framework.PlatformServletContext;13.importorg.eclipse.birt.core.framework.IPlatformContext;14.importorg.eclipse.birt.core.framework.Platform;15.importorg.eclipse.birt.core.exception.BirtException;16.importorg.eclipse.birt.report.engine.api.IReportEngineFactory;17.18.publicclassBirtEngine{19.privatestaticIReportEnginebirtEngine=null;20.privatestaticPropertiesconfigProps=newProperties();21.privatefinalstaticStringconfigFile="BirtCperties";22.protectedfinalstaticLoglog=LogFactory.getLog(BirtEngine.class);23. public static synchronized void initBirtConfig() {24.25. loadEngineProps();26. }27.28. public static synchronized IReportEngine getBirtEngine(ServletContext sc) {29.if(birtEngine==null){30.EngineConfigconfig=newEngineConfig();31.if(configProps!=null){32.StringlogLevel=configProps.getProperty("logLevel");33.Levellevel=Level.OFF;34.if("SEVERE".equalsIgnoreCase(logLevel)){35.level=Level.SEVERE;36.}elseif("WARNING".equalsIgnoreCase(logLevel)){37.level=Level.WARNING;38.}elseif("INFO".equalsIgnoreCase(logLevel)){39.level=Level.INFO;40.}elseif("CONFIG".equalsIgnoreCase(logLevel)){41.level=Level.CONFIG;42.}elseif("FINE".equalsIgnoreCase(logLevel)){43.level=Level.FINE;44.}elseif("FINER".equalsIgnoreCase(logLevel)){45.level=Level.FINER;46.}elseif("FINEST".equalsIgnoreCase(logLevel)){47.level=Level.FINEST;48.}elseif("OFF".equalsIgnoreCase(logLevel)){49.level=Level.OFF;50.}51.52..getProperty("logDirectory"),config.setLogConfig(configProps53.level);54.}55.//sc.getRealPath("")+java.io.File.separator+configProps.getProperty("engine_home")56.config.setEngineHome("");57.IPlatformContextcontext=newPlatformServletContext(sc);58.(context.getPlatform());59.config.setPlatformContext(context);60.61.try{62.Platform.startup(config);63.}catch(BirtExceptione){64.e.printStackTrace();65.}66.67.IReportEngineFactoryfactory=(IReportEngineFactory)Platform68
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026韩语升职面试题及答案
- 2026街舞面试题目及答案
- 江西省九江市六校2025-2026学年高二下学期期末考试化学试卷(含答案)
- 河南省许昌市2025-2026学年高一下学期期末质量检测语文试卷(含答案)
- 2026年铜陵学院公开招聘科研助理、管理助理、教学助理49名笔试模拟试题及答案详解
- 2026年乌兰察布市集宁区住房和城乡建设局人员招聘笔试参考试题及答案详解
- 2026年辽宁省大连市住房和城乡建设局人员招聘笔试备考试题及答案详解
- 2026年四川省雅安市住房和城乡建设局人员招聘考试备考题库及答案详解
- 2026浙江金温铁道公司领军紧缺人才招聘3人考试备考试题及答案详解
- 交易系统算力资源调度-第3篇
- 2026年石家庄市裕华区社区工作者招聘笔试参考试题及答案详解
- 【三年级下册英语】【人教PEP版】阅读理解专项训练91篇带答案
- 【安庆】2026年安徽桐城师范高等专科学校公开招聘工作人员8人笔试历年典型考题及考点剖析附带答案详解
- 2025太原五中高一英语分班考试真题含答案
- 《建筑施工模板安全技术规范》JGJ162-2025
- 2026年湖南省生物高考真题含答案
- SD高达G世纪 超越世界 金手指
- 2026年教师创新能力测试题及答案
- TSG08-2026《特种设备使用管理规则》解读
- GA/T 2131-2024移民管理领域标准体系表
- YBT 4334-2024《金属箔材 室温拉伸试验方法》
评论
0/150
提交评论