工具下载:https://pan.baidu.com/s/1pMEZwfh 1 创建一个Java项目 2 制定一个存放报表模板的包,创建report模板 new-->report,创建名为 mytest_1.rptdesign 的模板 3 创建数据源和数据集 3 新建数据源 和 数据集
方式一: 基于数据库sql 3.1 创建数据源 Data Sources 选择: JDBC Data Source 以mysql为例: Driver Class: com.mysql.jdbc.Driver (v5.1) Database URL: jdbc:mysql://192.168.101.75:13507/imp_monitordb 3.1 创建数据集 Data Sets Data Set Type 选择: SQL Select Query 然后写查询sql
创建数据源:
方式二 :基于代码,调用方法 3.1 创建数据源 Data Sources 选择: Scripted Data Source 3.1 创建数据集 Data Sets
布局及样式调整:
基本样式设置:
饼状图,柱状图和饼状图的数据集格式一样:
<page-setup> <simple-master-page name="Simple MasterPage" id="2"> <page-footer> <text id="3"> <property name="contentType">htmlproperty> <text-property name="content">new Date()]]> text-property> text> page-footer> simple-master-page> page-setup>
import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** * Created by test on 2018/2/6. */ public class test { public static IReportEngine engine ; //报表引擎 public static String birtRoot = "/usr/local/suninfo/siem/tomcat/webapps/ROOT/birt" ; //报表模版根路径 public static void main(String[] args) { Map<String,Object> paramValues=new HashMap<>(); paramValues.put("reportUuid","sabfaskjnmflwkem"); String outputFilePath=new DateTime().toString("yyyy-MM-dd"); createReport("logInfo_report_pdf.rptdesign",paramValues,birtRoot+"report/pdf/"+outputFilePath,"pdf"); } public static void createReport(String designPath,Map<String,Object> paramValues,String outFilePath,String fileFormat){ try{ //打开一个报表设计文件 String pathName=birtRoot+"/"+designPath; IReportRunnable design = engine.openReportDesign(pathName); //以打开的报表设计文件为参数,创建一个获取参数的对象 IGetParameterDefinitionTask paramTask = engine.createGetParameterDefinitionTask(design); //获取报表设计文件中的参数定义 Collection parameters = paramTask.getParameterDefns(false); HashMap parameterMap=evaluateParameterValues(parameters,paramValues); //创建生成报表的任务对象 IRunAndRenderTask task = engine.createRunAndRenderTask(design); //为报表生成任务设置参数集合对象 task.setParameterValues(parameterMap); if(fileFormat.equals("html")){ //创建报表render选项对象 HTMLRenderOption options = new HTMLRenderOption (); //设置输出格式(PDF,HTML等) options.setOutputFormat( fileFormat ); options.setImageDirectory(birtRoot+File.separatorChar+"html"+File.separatorChar+"image"); options.setOutputFileName(outFilePath); //设置render options对象 task.setRenderOption( options ); //运行生成报表 task.run(); task.close(); dealReportCss(birtRoot, outFilePath); }else if(fileFormat.equals("pdf")){ PDFRenderOption options = new PDFRenderOption(); options.setOutputFormat(fileFormat); options.setOutputFileName(outFilePath); task.setRenderOption( options ); //运行生成报表 task.run(); task.close(); } }catch (Exception e){ } } //为报表设计文件中定义的参数赋值 "rawtypes", "unchecked" }) ({ public static HashMap evaluateParameterValues(Collection paramDefns,Map<String,Object> params) { HashMap inputValues = new HashMap(); Iterator iter = paramDefns.iterator(); while (iter.hasNext()) { IParameterDefnBase pBase = (IParameterDefnBase) iter.next(); if (pBase instanceof IScalarParameterDefn) { IScalarParameterDefn paramDefn = (IScalarParameterDefn) pBase; String paramName = paramDefn.getName(); String inputValue = (String) params.get(paramName); inputValues.put(paramName, inputValue); } } return inputValues; } public static void dealReportCss(String birtRoot,String outFilePath){ RandomAccessFile random = null; FileOutputStream fos=null; OutputStreamWriter osw=null; BufferedWriter bw =null; try { if(!birtRoot.startsWith("/")){ birtRoot = "/" + birtRoot; } String str = "file:"+birtRoot.replace("\\", "/")+"/html"; StringBuffer sb = new StringBuffer(); random =new RandomAccessFile(new File(outFilePath),"rw"); String readLine = random.readLine(); while(readLine != null){ readLine = new String(readLine.getBytes("ISO-8859-1"), "UTF-8"); if(readLine.contains(str)){ readLine = readLine.replace(str, "."); } sb.append(readLine+"\n"); readLine = random.readLine(); } random.setLength(0); fos=new FileOutputStream(new File(outFilePath)); osw=new OutputStreamWriter(fos); bw = new BufferedWriter(osw); bw.write(sb.toString()); } catch (Exception e) { System.out.println(outFilePath+"读取报表文件异常!"); }finally{ try { if(random != null){ random.close(); } } catch (IOException e1) { e1.printStackTrace(); } try { if(bw != null){ bw.close(); } } catch (IOException e1) { e1.printStackTrace(); } try { if(osw != null){ osw.close(); } } catch (IOException e1) { e1.printStackTrace(); } try { if(fos != null){ fos.close(); } } catch (IOException e1) { e1.printStackTrace(); } } } }
<dependency> <groupId>org.apache.batikgroupId> <artifactId>cssartifactId> <version>1.6.0version> dependency> <dependency> <groupId>org.apache.batikgroupId> <artifactId>utilartifactId> <version>1.6.0version> dependency> <dependency> <groupId>org.eclipsegroupId> <artifactId>birt.runtimeartifactId> <version>4.5.0version> dependency> <dependency> <groupId>org.eclipsegroupId> <artifactId>core.runtimeartifactId> <version>3.11.0version> dependency> <dependency> <groupId>org.eclipsegroupId> <artifactId>core.resourcesartifactId> <version>3.10.0version> dependency> <dependency> <groupId>org.eclipse.datatools.connectivity.odagroupId> <artifactId>consumerartifactId> <version>3.2.6version> dependency> <dependency> <groupId>org.eclipse.datatools.connectivity.odagroupId> <artifactId>odaartifactId> <version>3.4.3version> dependency> <dependency> <groupId>org.eclipse.datatoolsgroupId> <artifactId>connectivityartifactId> <version>1.2.11version> dependency> <dependency> <groupId>org.eclipse.emfgroupId> <artifactId>commonartifactId> <version>2.11.0version> dependency> <dependency> <groupId>org.eclipse.emfgroupId> <artifactId>ecore.xmiartifactId> <version>2.11.0version> dependency> <dependency> <groupId>org.eclipse.emfgroupId> <artifactId>ecoreartifactId> <version>2.11.0version> dependency> <dependency> <groupId>org.eclipse.equinoxgroupId> <artifactId>commonartifactId> <version>3.7.0version> dependency> <dependency> <groupId>org.eclipse.equinoxgroupId> <artifactId>registryartifactId> <version>3.6.0version> dependency> <dependency> <groupId>org.eclipsegroupId> <artifactId>osgiartifactId> <version>3.10.100version> dependency> <dependency> <groupId>org.mozillagroupId> <artifactId>javascriptartifactId> <version>1.7.5version> dependency> <dependency> <groupId>org.w3c.cssgroupId> <artifactId>sacartifactId> <version>1.3.1version> dependency> <dependency> <groupId>com.ibmgroupId> <artifactId>icuartifactId> <version>54.1.1version> dependency> <dependency> <groupId>com.lowagiegroupId> <artifactId>textartifactId> <version>2.1.7version> dependency> <dependency> <groupId>org.apache.batikgroupId> <artifactId>transcoderartifactId> <version>1.6.0version> dependency> <dependency> <groupId>org.apache.batikgroupId> <artifactId>domartifactId> <version>1.6.2version> dependency> <dependency> <groupId>org.apache.batikgroupId> <artifactId>bridgeartifactId> <version>1.6.0version> dependency> <dependency> <groupId>org.apache.batikgroupId> <artifactId>dom.svgartifactId> <version>1.6.0version> dependency> <dependency> <groupId>org.apache.batikgroupId> <artifactId>parserartifactId> <version>1.6.0version> dependency> <dependency> <groupId>org.apache.batikgroupId> <artifactId>xmlartifactId> <version>1.6.0version> dependency> <dependency> <groupId>org.apache.batikgroupId> <artifactId>ext.awtartifactId> <version>1.6.0version> dependency> <dependency> <groupId>org.apachegroupId> <artifactId>xercesartifactId> <version>2.9.0version> dependency> <dependency> <groupId>org.w3c.domgroupId> <artifactId>svgartifactId> <version>1.1.0version> dependency> <dependency> <groupId>com.googlecode.juniversalchardetgroupId> <artifactId>juniversalchardetartifactId> <version>1.0.3version> dependency>