利用BIRT ReportEngine API开发报表
http://my.oschina.net/kzhou/blog/9172
birt报表部署方式有两种,一种是通过官方提供的WebViewerExample webapp去部署
另一种是通过ReportEngine API自行开发部署程序;
采用第一种的好处是不需要编写额外的代码,直接就能部署,比较方便,但是限制很多
如果采用第二种的话就很灵活了。
本日志主要记录采用ReportEngine API来进行报表的部署;
编码前准备:
下载birt-runtime-version.zip(www.eclipse.org有下载)
解压,其中ReportEngine目录存放着所有所需的东西
准备数据库驱动
编写birt报表文件
利用下边的代码就可以执行报表文件并生成目标html文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
Java代码 package com; import java.io.IOException; import java.util.HashMap; import java.util.logging.Level; import org.eclipse.birt.core.framework.Platform; import org.eclipse.birt.report.engine.api.EngineConfig; import org.eclipse.birt.report.engine.api.EngineException; import org.eclipse.birt.report.engine.api.HTMLActionHandler; import org.eclipse.birt.report.engine.api.HTMLEmitterConfig; import org.eclipse.birt.report.engine.api.HTMLRenderOption; import org.eclipse.birt.report.engine.api.HTMLServerImageHandler; import org.eclipse.birt.report.engine.api.IReportEngine; import org.eclipse.birt.report.engine.api.IReportEngineFactory; import org.eclipse.birt.report.engine.api.IReportRunnable; import org.eclipse.birt.report.engine.api.IRunAndRenderTask; import org.eclipse.birt.report.model.api.CellHandle; import org.eclipse.birt.report.model.api.DesignConfig; import org.eclipse.birt.report.model.api.DesignElementHandle; import org.eclipse.birt.report.model.api.ElementFactory; import org.eclipse.birt.report.model.api.GridHandle; import org.eclipse.birt.report.model.api.IDesignEngine; import org.eclipse.birt.report.model.api.IDesignEngineFactory; import org.eclipse.birt.report.model.api.ImageHandle; import org.eclipse.birt.report.model.api.LabelHandle; import org.eclipse.birt.report.model.api.ReportDesignHandle; import org.eclipse.birt.report.model.api.RowHandle; import org.eclipse.birt.report.model.api.SessionHandle; import org.eclipse.birt.report.model.api.activity.SemanticException; import com.ibm.icu.util.ULocale; /** * Simple BIRT Design Engine API (DEAPI) demo. * 不保存到文件,就直接解析和生成 */ @SuppressWarnings("deprecation") public class BIRT { public static IReportEngine birtReportEngine = null; public static void makeDesign(ReportDesignHandle design, ElementFactory efactory) throws Exception { DesignElementHandle element = efactory.newSimpleMasterPage("Page Master"); design.getMasterPages().add(element); GridHandle grid = efactory.newGridItem(null, 2, 1); design.getBody().add(grid); grid.setWidth("100%"); RowHandle row = (RowHandle) grid.getRows().get(0); ImageHandle image = efactory.newImage(null); image.setURL("\"1.jpg\""); CellHandle cell = (CellHandle) row.getCells().get(0); cell.getContent().add(image); LabelHandle label = efactory.newLabel(null); label.setText("Hello, world!"); cell = (CellHandle) row.getCells().get(1); cell.getContent().add(label); } static ReportDesignHandle buildReport() throws IOException, SemanticException { DesignConfig config = new DesignConfig(); config.setProperty("BIRT_HOME", Utils.BIRT_HOME); IDesignEngine engine = null; try { Platform.startup(config); IDesignEngineFactory factory = (IDesignEngineFactory) Platform.createFactoryObject(IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY); engine = factory.createDesignEngine(config); } catch (Exception ex) { ex.printStackTrace(); } SessionHandle session = engine.newSessionHandle(ULocale.ENGLISH); ReportDesignHandle design = session.createDesign(); ElementFactory efactory = design.getElementFactory(); try { makeDesign(design, efactory); } catch (Exception e) { e.printStackTrace(); return null; } return design; } @SuppressWarnings({ "unchecked" }) public static void executeReport(ReportDesignHandle reportDesignHaddle) throws EngineException { HashMap<String, String> parameters = new HashMap<String, String>(); String name = "byCondition"; String pvalue = "3"; parameters.put(name, pvalue); IReportEngine engine = null; EngineConfig config = null; try { // 设置Engine并且启动报表平台 config = new EngineConfig(); config.setEngineHome(Utils.BIRT_HOME); // 设置报表日志保存的位置和等级( null, Level ) 如果你不需要日志可以设置为null config.setLogConfig("c:/birt/logs", Level.FINE); // 平台初始化,启用 Platform.startup(config); IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY); engine = factory.createReportEngine(config); engine.changeLogLevel(Level.WARNING); } catch (Exception ex) { ex.printStackTrace(); } // 设置发起者的一些操作,比如显示图片,报表生成到html页面,很关键的部分 HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig(); emitterConfig.setActionHandler(new HTMLActionHandler()); HTMLServerImageHandler imageHandler = new HTMLServerImageHandler(); emitterConfig.setImageHandler(imageHandler); config.getEmitterConfigs().put("html", emitterConfig); //$NON-NLS-1$ IReportRunnable design = null; // 打开设计好的报表,取绝对路径,最好使用context.getRealPath();这种方法实现,官方这个比较呆 // design = engine.openReportDesign(Utils.SAVE_HOME + fileName + ".rptdesign"); //不保存到文件,就直接解析和生成 design = engine.openReportDesign(reportDesignHaddle); // 创建报表任务 IRunAndRenderTask task = engine.createRunAndRenderTask(design); // // 设置报表的路径和图片显示的路径 // HTMLRenderContext renderContext = new HTMLRenderContext(); // // 为所有的actions设置Base URL,这个不写就是默认服务器URL的 // renderContext.setBaseURL("http://localhost/"); // // 设置所有图片显示的URL - 如果之前没有emitterConfig.setImageHandler( imageHandler // // );的话会造成显示的URL是本地的绝对路径,其实http://localhost不写也是可以的,会自动添加服务器的URL // renderContext.setBaseImageURL("http://localhost/myimages"); // // 设置所有图片存放的位置,最好使用context.getRealPath(); // renderContext.setImageDirectory("C:/xampplite/htdocs/myimages"); // // 设置图片支持的格式,据官方说必须有SVG,我没写也没出错 // renderContext.setSupportedImageFormats("JPG;PNG;BMP;SVG"); // HashMap<String, HTMLRenderContext> contextMap = new HashMap<String, HTMLRenderContext>(); // contextMap.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, // renderContext); // task.setAppContext(contextMap); // 设置参数 task.setParameterValues(parameters); // 要所有的参数一条一条的写入,例如: task.setParameterValue("Top Count", new // Integer(12)); task.validateParameters(); // 增加scrpit参考下面的例子 // pFilter.myjavamethod() // ProcessFilter pf = new ProcessFilter(); // task.addScriptableJavaObject("pFilter", pf); // 设置rendering操作- 例如file or stream output, output format, whether it is // embeddable, etc HTMLRenderOption options = new HTMLRenderOption(); // 例如:Remove HTML and Body tags // options.setEmbeddable(true); // 设置输出本地文件 options.setOutputFileName("D:\\dev-workspace\\workspace-v4.0\\BIRT\\temp\\test.html"); // 设置输出文件格式 options.setOutputFormat("html"); task.setRenderOption(options); // 运行report任务,然后关闭 // 如果要长期留驻的话可以不关闭,我建议不关闭engine和Platform,要不每次打开报表都要等好久…… task.run(); task.close(); engine.shutdown(); Platform.shutdown(); } /** * 不保存到文件,就直接解析和生成 * @param args */ public static void main(String[] args) { birtReportEngine = BirtEngine.getBirtEngine(); try { ReportDesignHandle reportDesignHaddle = buildReport(); System.out.println("产生完成"); executeReport(reportDesignHaddle); System.out.println("导出完成"); reportDesignHaddle.close(); } catch (Exception e) { e.printStackTrace(); } } } |