一、概念
BIRT报表工具是一个开源的、基于Eclipse的报表生成工具,专门用于为企业应用生成各种格式的报表。它能够帮助开发人员在Java应用中嵌入报告和数据分析功能,提供丰富的报表设计、数据集成和视图呈现能力。BIRT广泛应用于企业的数据报告、业务智能、图表可视化等领域。
能够从各种数据源(如数据库、Web服务、XML、CSV等)获取数据,并基于这些数据生成图表、报表、仪表板等多种数据可视化形式。其核心功能包括报表设计、数据集成、图表生成、定制报表输出等。
二、BIRT由两部分组成
先设计,再运行
- BIRT报表设计器:一个图形化界面的工具,用于设计报表的布局、格式、数据源等内容。它通常作为Eclipse插件使用,也可以独立下载使用。
- BIRT运行时:报表设计完成后,可以在Java应用中通过BIRT运行时库来执行生成报表并输出结果。运行时可以嵌入到Web应用中,通过Web浏览器显示报表,或生成PDF、Excel等格式的报告。
三、BIRT对Java项目的好处
BIRT报表工具对Java项目有许多好处,特别是在需要生成业务报表和数据分析时:
- 易于集成:BIRT能够很方便地集成到现有的Java应用中,包括Java Web应用和Java EE应用。它提供了运行时库,可以嵌入到Java代码中执行报表生成和导出功能。
- 丰富的数据可视化:BIRT支持多种数据可视化方式,如图表(饼图、条形图、折线图等)、交叉表(Crosstab)、数据表等。你可以根据需要灵活选择图表类型,以更好地展示业务数据。
- 支持多种数据源:BIRT支持从多种数据源提取数据,包括关系型数据库(SQL)、Web服务、文件(XML、CSV等)等,灵活的数据源支持让它能广泛应用于不同场景。
- 强大的报表定制功能:通过BIRT的脚本功能(如JavaScript),开发人员可以实现更复杂的报表逻辑,比如动态过滤、条件格式化、数据计算等。
- 支持多种输出格式:BIRT支持多种报表输出格式,如PDF、HTML、Excel、Word等,方便用户根据不同需求选择最适合的格式。
- 开源且免费:BIRT是开源的,遵循Eclipse公共许可证(EPL),可以自由使用并进行二次开发,避免了高额的商业授权费用。
四、BIRT是全自动的吗?
BIRT并不是完全“自动化”的报表工具。它需要开发人员在报表设计和集成过程中进行一些手动配置和操作。具体来说:
- 报表设计:虽然BIRT提供了可视化的设计器,但设计报表的布局、数据源、查询等内容依然需要手动配置。
- 数据处理:BIRT从数据源获取数据并将其渲染为报表,但这个过程也需要开发人员指定数据源、编写SQL查询等,不能完全自动化。
- 生成与导出:报表生成和导出可以通过BIRT的运行时库在Java应用中自动化完成,但这通常需要一定的编程工作来触发报表生成,并控制报表的参数和输出格式。
因此,BIRT为报表的生成和集成提供了很强的工具支持,但仍需要开发人员配置和操作,无法完全自动化。
五、下载BIRT4.3.1
https://www.genuitec.com/forums/topic/reports-project-deploying-to-tomcat-sha1-digest-error/
六、环境配置
- 解压缩,先拷贝下图三个,到工程的目录D:\yun\guoji-his\src\main\webapp\WEB-INF下,我这目录有,内容一样的,不复制了
- 将birt.tld复制到工程的目录D:\yun\guoji-his\src\main\webapp\WEB-INF\tlds下,我这目录有,内容一样的,不复制了
- 在工程的目录D:\yun\guoji-his\src\main\webapp\WEB-INF下,建立report-engine文件夹,在这里面建立以下四个文件夹
- 将birt-runtime-4_3_1\WebViewerExample\webcontent文件夹,复制到web应用的根目录下
- 在web应用的根目录下建立reportFiles文件夹,用来存报表文件
- 将web.xml中的以下内容,合并到工程的web.xml中
<display-name>Eclipse BIRT Report Viewer</display-name>
<!-- Default locale setting.
-->
<context-param>
<param-name>BIRT_VIEWER_LOCALE</param-name>
<param-value>en-US</param-value>
</context-param>
<!--
Default timezone setting.
Examples: "Europe/Paris", "GMT+1".
Defaults to the container's timezone.
-->
<context-param>
<param-name>BIRT_VIEWER_TIMEZONE</param-name>
<param-value></param-value>
</context-param>
<!--
Report resources directory for preview. Defaults to ${birt home}
-->
<context-param>
<param-name>BIRT_VIEWER_WORKING_FOLDER</param-name>
<param-value></param-value>
</context-param>
<!--
Temporary document files directory. Defaults to ${birt home}/documents
-->
<context-param>
<param-name>BIRT_VIEWER_DOCUMENT_FOLDER</param-name>
<param-value></param-value>
</context-param>
<!--
Flag whether the report resources can only be accessed under the
working folder. Defaults to true
-->
<context-param>
<param-name>WORKING_FOLDER_ACCESS_ONLY</param-name>
<param-value>true</param-value>
</context-param>
<!--
Settings for how to deal with the url report path. e.g. "http://host/repo/test.rptdesign".
Following values are supported:
<all> - All paths.
<domain> - Only the paths with host matches current domain. Note the comparison is literal, "127.0.0.1" and "localhost" are considered as different hosts.
<none> - URL paths are not supported.
Defaults to "domain".
-->
<context-param>
<param-name>URL_REPORT_PATH_POLICY</param-name>
<param-value>domain</param-value>
</context-param>
<!--
Temporary image/chart directory. Defaults to ${birt home}/report/images
-->
<context-param>
<param-name>BIRT_VIEWER_IMAGE_DIR</param-name>
<param-value></param-value>
</context-param>
<!-- Engine log directory. Defaults to ${birt home}/logs -->
<context-param>
<param-name>BIRT_VIEWER_LOG_DIR</param-name>
<param-value></param-value>
</context-param>
<!-- Report engine log level -->
<context-param>
<param-name>BIRT_VIEWER_LOG_LEVEL</param-name>
<param-value>WARNING</param-value>
</context-param>
<!--
Directory where to store all the birt report script libraries (JARs).
Defaults to ${birt home}/scriptlib
-->
<context-param>
<param-name>BIRT_VIEWER_SCRIPTLIB_DIR</param-name>
<param-value></param-value>
</context-param>
<!-- Resource location directory. Defaults to ${birt home} -->
<context-param>
<param-name>BIRT_RESOURCE_PATH</param-name>
<param-value></param-value>
</context-param>
<!-- Preview report rows limit. An empty value means no limit. -->
<context-param>
<param-name>BIRT_VIEWER_MAX_ROWS</param-name>
<param-value></param-value>
</context-param>
<!--
Max cube fetch levels limit for report preview (Only used when
previewing a report design file using the preview pattern)
-->
<context-param>
<param-name>BIRT_VIEWER_MAX_CUBE_ROWLEVELS</param-name>
<param-value></param-value>
</context-param>
<context-param>
<param-name>BIRT_VIEWER_MAX_CUBE_COLUMNLEVELS</param-name>
<param-value></param-value>
</context-param>
<!-- Memory size in MB for creating a cube. -->
<context-param>
<param-name>BIRT_VIEWER_CUBE_MEMORY_SIZE</param-name>
<param-value></param-value>
</context-param>
<!-- Defines the BIRT viewer configuration file -->
<context-param>
<param-name>BIRT_VIEWER_CONFIG_FILE</param-name>
<param-value>WEB-INF/viewer.properties</param-value>
</context-param>
<!--
Flag whether to allow server-side printing. Possible values are "ON"
and "OFF". Defaults to "ON".
-->
<context-param>
<param-name>BIRT_VIEWER_PRINT_SERVERSIDE</param-name>
<param-value>ON</param-value>
</context-param>
<!--
Flag whether to force browser-optimized HTML output. Defaults to true
-->
<context-param>
<param-name>HTML_ENABLE_AGENTSTYLE_ENGINE</param-name>
<param-value>true</param-value>
</context-param>
<!--
Filename generator class/factory to use for the exported reports.
-->
<context-param>
<param-name>BIRT_FILENAME_GENERATOR_CLASS</param-name>
<param-value>org.eclipse.birt.report.utility.filename.DefaultFilenameGenerator</param-value>
</context-param>
<!--
Viewer Filter used to set the request character encoding to UTF-8.
-->
<filter>
<filter-name>ViewerFilter</filter-name>
<filter-class>org.eclipse.birt.report.filter.ViewerFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ViewerFilter</filter-name>
<servlet-name>ViewerServlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>ViewerFilter</filter-name>
<servlet-name>EngineServlet</servlet-name>
</filter-mapping>
<!-- Viewer Servlet Context Listener -->
<listener>
<listener-class>org.eclipse.birt.report.listener.ViewerServletContextListener</listener-class>
</listener>
<!-- Viewer HttpSession Listener -->
<listener>
<listener-class>org.eclipse.birt.report.listener.ViewerHttpSessionListener</listener-class>
</listener>
<!-- Viewer Servlet, Supports SOAP -->
<servlet>
<servlet-name>ViewerServlet</servlet-name>
<servlet-class>org.eclipse.birt.report.servlet.ViewerServlet</servlet-class>
</servlet>
<!-- Engine Servlet -->
<servlet>
<servlet-name>EngineServlet</servlet-name>
<servlet-class>org.eclipse.birt.report.servlet.BirtEngineServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ViewerServlet</servlet-name>
<url-pattern>/frameset</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ViewerServlet</servlet-name>
<url-pattern>/run</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/preview</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/download</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/parameter</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/document</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/output</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/extract</url-pattern>
</servlet-mapping>
<jsp-config>
<taglib>
<taglib-uri>/birt.tld</taglib-uri>
<taglib-location>/WEB-INF/tlds/birt.tld</taglib-location>
</taglib>
</jsp-config>
其中web.xml文件需做如下修改:
- a、修改BIRT_VIEWER_WORKING_FOLDER项的值为reportFiles;
- b、修改BIRT_VIEWER_DOCUMENT_FOLDER项的值为WEB-INF/report-engine/documents;
- c、修改BIRT_VIEWER_IMAGE_DIR项的值为WEB-INF/report-engine/images;
- d、修改BIRT_VIEWER_LOG_DIR项的值为WEB-INF/report-engine/logs;
这里有个注意事项就是,这个日志文件如果配置了地址的话,到时候每次生成报表的时候,都会生成一批日志文件,会占用服务器资源。一个办法是定期的删除这个下面的文件,还有一个办法就是不配置日志文件地址,我就是采用第二种方法的。 - e、修改BIRT_VIEWER_SCRIPTLIB_DIR项的值为WEB-INF/report-engine/scriptlib;
- f、如果需调整日志级别可修改BIRT_VIEWER_LOG_LEVEL的值为ALL;
可选的值有:ALL|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST|OFF。级别由高到低。
我合并且修改后的xml如下:
<?xml version="1.0" encoding="utf-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
version="2.4"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>myareahisjlnyb</display-name>
<!-- Spring框架配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,classpath:spring-hibernate.xml</param-value>
</context-param>
<!-- BIRT Report Viewer配置 -->
<!-- Default locale setting -->
<context-param>
<param-name>BIRT_VIEWER_LOCALE</param-name>
<param-value>zh_CN</param-value>
</context-param>
<!-- Default timezone setting -->
<context-param>
<param-name>BIRT_VIEWER_TIMEZONE</param-name>
<param-value></param-value>
</context-param>
<!-- Report resources directory for preview -->
<context-param>
<param-name>BIRT_VIEWER_WORKING_FOLDER</param-name>
<param-value>reportFiles</param-value>
</context-param>
<!-- Temporary document files directory -->
<context-param>
<param-name>BIRT_VIEWER_DOCUMENT_FOLDER</param-name>
<param-value>WEB-INF/report-engine/documents</param-value>
</context-param>
<!-- Flag whether the report resources can only be accessed under the working folder -->
<context-param>
<param-name>WORKING_FOLDER_ACCESS_ONLY</param-name>
<param-value>true</param-value>
</context-param>
<!-- Settings for how to deal with the url report path -->
<context-param>
<param-name>URL_REPORT_PATH_POLICY</param-name>
<param-value>domain</param-value>
</context-param>
<!-- Temporary image/chart directory -->
<context-param>
<param-name>BIRT_VIEWER_IMAGE_DIR</param-name>
<param-value>WEB-INF/report-engine/images</param-value>
</context-param>
<!-- Engine log directory -->
<context-param>
<param-name>BIRT_VIEWER_LOG_DIR</param-name>
<param-value>WEB-INF/report-engine/logs</param-value>
</context-param>
<!-- Report engine log level -->
<context-param>
<param-name>BIRT_VIEWER_LOG_LEVEL</param-name>
<param-value>ALL</param-value>
</context-param>
<!-- Directory where to store all the birt report script libraries (JARs) -->
<context-param>
<param-name>BIRT_VIEWER_SCRIPTLIB_DIR</param-name>
<param-value>WEB-INF/report-engine/scriptlib</param-value>
</context-param>
<!-- Resource location directory -->
<context-param>
<param-name>BIRT_RESOURCE_PATH</param-name>
<param-value></param-value>
</context-param>
<!-- Preview report rows limit -->
<context-param>
<param-name>BIRT_VIEWER_MAX_ROWS</param-name>
<param-value></param-value>
</context-param>
<!-- Max cube fetch levels limit for report preview -->
<context-param>
<param-name>BIRT_VIEWER_MAX_CUBE_ROWLEVELS</param-name>
<param-value></param-value>
</context-param>
<context-param>
<param-name>BIRT_VIEWER_MAX_CUBE_COLUMNLEVELS</param-name>
<param-value></param-value>
</context-param>
<!-- Memory size in MB for creating a cube -->
<context-param>
<param-name>BIRT_VIEWER_CUBE_MEMORY_SIZE</param-name>
<param-value></param-value>
</context-param>
<!-- Defines the BIRT viewer configuration file -->
<context-param>
<param-name>BIRT_VIEWER_CONFIG_FILE</param-name>
<param-value>WEB-INF/viewer.properties</param-value>
</context-param>
<!-- Flag whether to allow server-side printing -->
<context-param>
<param-name>BIRT_VIEWER_PRINT_SERVERSIDE</param-name>
<param-value>ON</param-value>
</context-param>
<!-- Flag whether to force browser-optimized HTML output -->
<context-param>
<param-name>HTML_ENABLE_AGENTSTYLE_ENGINE</param-name>
<param-value>true</param-value>
</context-param>
<!-- Filename generator class/factory to use for the exported reports -->
<context-param>
<param-name>BIRT_FILENAME_GENERATOR_CLASS</param-name>
<param-value>org.eclipse.birt.report.utility.filename.DefaultFilenameGenerator</param-value>
</context-param>
<!-- Listeners -->
<listener>
<listener-class>org.eclipse.birt.report.listener.ViewerServletContextListener</listener-class>
</listener>
<listener>
<listener-class>org.eclipse.birt.report.listener.ViewerHttpSessionListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>areahis.util.SysContextListener</listener-class>
</listener>
<!-- Filters -->
<filter>
<filter-name>ViewerFilter</filter-name>
<filter-class>org.eclipse.birt.report.filter.ViewerFilter</filter-class>
</filter>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter>
<filter-name>SecurityFilter</filter-name>
<filter-class>areahis.filter.SecurityFilter</filter-class>
<init-param>
<param-name>sessionname</param-name>
<param-value>sessioninfo</param-value>
</init-param>
<init-param>
<param-name>excludefile</param-name>
<param-value>css,js,jpg,png,gif,bmp,swf,rar,pdf,doc,docx,html,ttf,woff,wasm</param-value>
</init-param>
</filter>
<!-- Filter Mappings -->
<filter-mapping>
<filter-name>ViewerFilter</filter-name>
<servlet-name>ViewerServlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>ViewerFilter</filter-name>
<servlet-name>EngineServlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>SecurityFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Servlets -->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>EngineServlet</servlet-name>
<servlet-class>org.eclipse.birt.report.servlet.BirtEngineServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>ViewerServlet</servlet-name>
<servlet-class>org.eclipse.birt.report.servlet.ViewerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Servlet Mappings -->
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ViewerServlet</servlet-name>
<url-pattern>/frameset</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ViewerServlet</servlet-name>
<url-pattern>/run</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/preview</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/download</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/parameter</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/document</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/output</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EngineServlet</servlet-name>
<url-pattern>/extract</url-pattern>
</servlet-mapping>
<!-- JSP Config -->
<jsp-config>
<taglib>
<taglib-uri>/birt.tld</taglib-uri>
<taglib-location>/WEB-INF/tlds/birt.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
- 下面就是最重要的步骤了,Maven中jar包的引入,到底4.3.1需要引入多少包,以及哪些包,如下:
<dependency>
<groupId>org.eclipse.birt.runtime</groupId>
<artifactId>org.eclipse.birt.runtime</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxrpc</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.birt.runtime</groupId>
<artifactId>com.ibm.icu</artifactId>
<version>50.1.1.v201304230130</version>
</dependency>
<dependency>
<groupId>org.eclipse.birt.runtime</groupId>
<artifactId>org.eclipse.core.runtime</artifactId>
<version>3.9.0.v20130326-1255</version>
</dependency>
<dependency>
<groupId>org.eclipse.core</groupId>
<artifactId>org.eclipse.core.runtime</artifactId>
<version>3.6.200.v20130402-1505</version>
</dependency>
<dependency>
<groupId>org.eclipse.birt.runtime</groupId>
<artifactId>org.eclipse.equinox.registry</artifactId>
<version>3.5.301.v20130717-1549</version>
</dependency>
<dependency>
<groupId>org.eclipse.birt.runtime</groupId>
<artifactId>org.eclipse.datatools.connectivity</artifactId>
<version>1.2.9.v201307261105</version>
</dependency>
<dependency>
<groupId>org.eclipse.birt.runtime</groupId>
<artifactId>org.eclipse.datatools.connectivity.oda</artifactId>
<version>3.4.1.v201308160907</version>
</dependency>
<dependency>
<groupId>org.eclipse.birt.runtime</groupId>
<artifactId>org.eclipse.datatools.connectivity.oda.consumer</artifactId>
<version>3.2.6.v201305170644</version>
</dependency>
<dependency>
<groupId>org.eclipse.birt.runtime</groupId>
<artifactId>org.eclipse.osgi</artifactId>
<version>3.9.1.v20130814-1242</version>
</dependency>
<dependency>
<groupId>com.hfmx</groupId>
<artifactId>Tidy</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.birt.runtime</groupId>
<artifactId>viewservlets</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.birt.runtime.3_7_1</groupId>
<artifactId>org.w3c.css.sac</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.birt.runtime.3_7_1</groupId>
<artifactId>org.apache.batik.css</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.birt.runtime.3_7_1</groupId>
<artifactId>org.apache.batik.util</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.birt.runtime.3_7_1</groupId>
<artifactId>org.apache.xerces</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.birt.runtime.3_7_1</groupId>
<artifactId>com.lowagie.text</artifactId>
<version>2.1.7</version>
</dependency>
所有的这些包,在下载的环境中都有,如果maven管理中没有的依赖的话,需要手动注册,注册方式如下:
mvn install:install-file -Dfile=e:/Tidy.jar -DgroupId=com.hfmx -DartifactId=Tidy -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true