工具下载: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
创建数据源:

创建数据集不带参数的sql:

带参数的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">html
property> <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.batik
groupId> <artifactId>css
artifactId> <version>1.6.0
version>
dependency> <dependency> <groupId>org.apache.batik
groupId> <artifactId>util
artifactId> <version>1.6.0
version>
dependency> <dependency> <groupId>org.eclipse
groupId> <artifactId>birt.runtime
artifactId> <version>4.5.0
version>
dependency> <dependency> <groupId>org.eclipse
groupId> <artifactId>core.runtime
artifactId> <version>3.11.0
version>
dependency> <dependency> <groupId>org.eclipse
groupId> <artifactId>core.resources
artifactId> <version>3.10.0
version>
dependency> <dependency> <groupId>org.eclipse.datatools.connectivity.oda
groupId> <artifactId>consumer
artifactId> <version>3.2.6
version>
dependency> <dependency> <groupId>org.eclipse.datatools.connectivity.oda
groupId> <artifactId>oda
artifactId> <version>3.4.3
version>
dependency> <dependency> <groupId>org.eclipse.datatools
groupId> <artifactId>connectivity
artifactId> <version>1.2.11
version>
dependency> <dependency> <groupId>org.eclipse.emf
groupId> <artifactId>common
artifactId> <version>2.11.0
version>
dependency> <dependency> <groupId>org.eclipse.emf
groupId> <artifactId>ecore.xmi
artifactId> <version>2.11.0
version>
dependency> <dependency> <groupId>org.eclipse.emf
groupId> <artifactId>ecore
artifactId> <version>2.11.0
version>
dependency> <dependency> <groupId>org.eclipse.equinox
groupId> <artifactId>common
artifactId> <version>3.7.0
version>
dependency> <dependency> <groupId>org.eclipse.equinox
groupId> <artifactId>registry
artifactId> <version>3.6.0
version>
dependency> <dependency> <groupId>org.eclipse
groupId> <artifactId>osgi
artifactId> <version>3.10.100
version>
dependency> <dependency> <groupId>org.mozilla
groupId> <artifactId>javascript
artifactId> <version>1.7.5
version>
dependency> <dependency> <groupId>org.w3c.css
groupId> <artifactId>sac
artifactId> <version>1.3.1
version>
dependency> <dependency> <groupId>com.ibm
groupId> <artifactId>icu
artifactId> <version>54.1.1
version>
dependency> <dependency> <groupId>com.lowagie
groupId> <artifactId>text
artifactId> <version>2.1.7
version>
dependency> <dependency> <groupId>org.apache.batik
groupId> <artifactId>transcoder
artifactId> <version>1.6.0
version>
dependency> <dependency> <groupId>org.apache.batik
groupId> <artifactId>dom
artifactId> <version>1.6.2
version>
dependency> <dependency> <groupId>org.apache.batik
groupId> <artifactId>bridge
artifactId> <version>1.6.0
version>
dependency> <dependency> <groupId>org.apache.batik
groupId> <artifactId>dom.svg
artifactId> <version>1.6.0
version>
dependency> <dependency> <groupId>org.apache.batik
groupId> <artifactId>parser
artifactId> <version>1.6.0
version>
dependency> <dependency> <groupId>org.apache.batik
groupId> <artifactId>xml
artifactId> <version>1.6.0
version>
dependency> <dependency> <groupId>org.apache.batik
groupId> <artifactId>ext.awt
artifactId> <version>1.6.0
version>
dependency> <dependency> <groupId>org.apache
groupId> <artifactId>xerces
artifactId> <version>2.9.0
version>
dependency> <dependency> <groupId>org.w3c.dom
groupId> <artifactId>svg
artifactId> <version>1.1.0
version>
dependency> <dependency> <groupId>com.googlecode.juniversalchardet
groupId> <artifactId>juniversalchardet
artifactId> <version>1.0.3
version>
dependency>
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/178333.html原文链接:https://javaforall.net
