eclipse+birt報表開發
阿新 • • 發佈:2018-12-10
工具下載: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-footer>標籤即可:
<page-setup>
<simple-master-page name="Simple MasterPage" id="2">
< page-footer>
<text id="3">
<property name="contentType">html</property>
<text-property name="content"><![CDATA[<value-of>new Date()</value-of>]]> </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(); } } } }
<!-- 報表相關jar -->
<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>