JasperReport html 导出
In my last blog post I discussed about Generating jasper reports in different formats using json file as a data source.You can find my last post here. In this blog article I will discuss about exporting the jasperPrint object in different formats like pdf, html, csv, xls and docx using the newer API of jasper reports.
Exporting the jasperPrint object which consists of images into html format is a tedious task with the jasper’s deprecated API found all over the internet. I have tried using the JRHtmlExporter class that consists of mostly deprecated methods and using it I couldn’t get the images in the jrxml in the html format.
So, I wanted to write a blog post to help my fellow programmers to illustrate how it can be done with the new API of jasper reports. HtmlExporter class is part of new API and using it one can export the report to html format.
To do this first we need to place the jasperPrint object in the http session using the following code.
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
After placing the jasperPrint object in the session, we need to set the image handler for images in the report using the code,
exporterOutput.setImageHandler(new WebHtmlResourceHandler("image?image={0}"));
The images are served by ImageServlet which should be mapped to ‘/image’ in the web.xml. Here is the configuration that should be added to web.xml file.
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class> </servlet>
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/image</url-pattern>
</servlet-mapping>
The JasperCompileManager.compileReport(jrxmlSource)
method compiles and generates a jasperReport object which is used to generate jasperPrint object using the JasperFillManager.fillReport(jasperReport, parameters, dataSource)
method. In the following example the dataSource is populated using a string which is in json format. I am exporting the generated documents to the response. So, accordingly I have set content-type, content-disposition headers appropriately, which I have not shown in the code. The headers are set for all formats except for the type html as htmls are to be displayed in the browser along with images.
You can refer my previous blog post for maven dependencies. I have used commons-io dependency in addition to the previous dependencies.
private static final Logger logger = LoggerFactory.getLogger(YOURCLASS.class);
JRDataSource dataSource = getDataSource(jsonData);//pass jsonData to populate the dataSource
JasperReport jasperReport = null;
JasperPrint jasperPrint = null;
//String type = Any of the types mentioned above
//jrxmlSource is the the jrxml generated using the iReport Map<String, Object> parameters = new HashMap<String, Object>();
//Add any parameters that are referenced in the jrxml to this map try {
jasperReport = JasperCompileManager.compileReport(jRXMLSource);
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
} catch (JRException ex) {
ex.printStackTrace();
} if ("pdf".equals(type)) {
JRPdfExporter exporter = new JRPdfExporter();
try {
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
exporter.exportReport();
} catch (IOException e) {
logger.error("IOException occured", e);
e.printStackTrace();
} catch (JRException e) {
logger.error("JRException while exporting for pdf format", e);
e.printStackTrace();
} } else if ("xls".equals(type)) { JRXlsExporter exporter = new JRXlsExporter();
try {
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
configuration.setOnePagePerSheet(true);
exporter.setConfiguration(configuration);
exporter.exportReport();
} catch (JRException e) {
logger.error("JRException while exporting for xls format", e);
e.printStackTrace();
} catch (IOException e) {
logger.error("IOException occured", e);
e.printStackTrace();
} } else if ("csv".equals(type)) {
JRCsvExporter exporter = new JRCsvExporter();
try {
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleWriterExporterOutput(response.getOutputStream()));
exporter.exportReport();
} catch (IOException e) {
logger.error("IOException occured", e);
e.printStackTrace();
} catch (JRException e) {
logger.error("JRException while exporting report csv format", e);
e.printStackTrace();
}
} else if ("html".equals(type)) {
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,jasperPrint); HtmlExporter exporterHTML = new HtmlExporter();
SimpleExporterInput exporterInput = new SimpleExporterInput(jasperPrint);
exporterHTML.setExporterInput(exporterInput); SimpleHtmlExporterOutput exporterOutput;
try {
exporterOutput = new SimpleHtmlExporterOutput(response.getOutputStream());
exporterOutput.setImageHandler(new WebHtmlResourceHandler("image?image={0}"));
exporterHTML.setExporterOutput(exporterOutput); SimpleHtmlReportConfiguration reportExportConfiguration = new SimpleHtmlReportConfiguration();
reportExportConfiguration.setWhitePageBackground(false);
reportExportConfiguration.setRemoveEmptySpaceBetweenRows(true);
exporterHTML.setConfiguration(reportExportConfiguration);
exporterHTML.exportReport();
} catch (IOException e) {
logger.error("IOException occured", e);
e.printStackTrace();
} catch (JRException e) {
logger.error("JRException while exporting for html format", e);
e.printStackTrace();
}
} else if ("docx".equals(type)) {
JRDocxExporter exporter = new JRDocxExporter(); try {
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
exporter.exportReport();
} catch (IOException e) {
logger.error("IOException occured", e);
e.printStackTrace();
} catch (JRException e) {
logger.error("JRException while exporting for docx format", e);
e.printStackTrace();
}
} public JRDataSource getDataSource(String jsonData) {
logger.info("jsonData = " + jsonData);
JRDataSource dataSource = null; if ("null".equals(jsonData) || jsonData == null || "".equals(jsonData)) {
logger.info("jsonData parameter value is null. Creating JREmptyDataSource");
dataSource = new JREmptyDataSource();
return dataSource;
} InputStream jsonInputStream = null;
try {
// Convert the jsonData string to inputStream
jsonInputStream = IOUtils.toInputStream(jsonData, "UTF-8");
// selectExpression is based on the jsonData that your string contains
dataSource = new JsonDataSource(jsonInputStream, "data");
} catch (IOException ex) {
logger.error("Couldn't covert string into inputStream", ex);
ex.printStackTrace();
} catch (JRException e) {
logger.error("Couldn't create JsonDataSource", e);
e.printStackTrace();
} if (dataSource == null) {
dataSource = new JREmptyDataSource();
logger.info("dataSource is null. Request parameter jsondData is null");
} return dataSource;
}
Hope the above code helps to resolve the issue of getting the images in html format. The images can be placed in WEB-INF/classes directory if the directory is not mentioned in the jrxml. If the directory is mentioned then the path should supplied as a parameter which should be kept inside parameters map.
Wish you happy coding!!
Rajasekhar
Helical IT Solutions
JasperReport html 导出的更多相关文章
- JasperReport报表导出踩坑实录
写在最前面 翻了翻博客,因为太忙,已经好久没认真总结过了. 正好趁着今天老婆出门团建的机会,记录下最近这段时间遇到的大坑-JasperReport. 六月份的时候写过一篇利用poi文件导入导出的小De ...
- 利用JasperReport+iReport进行Web报表开发
用JasperReport+iReport进行Web报表开发 序言 在非常多实际的项目里,报表都是当中十分重要的组成部分,比如把查询结果以报表的形式呈现出来.这里所提到的报表可不是简单的二维表,而是拥 ...
- 使用JasperReport+iReport进行Web报表开发
使用JasperReport+iReport进行Web报表开发 前言 在实际工程中非常,报告是其中很重要的一部分,结果以报表的形式呈现出来.这里所提到的报表可不是简单的二维表,而是拥有复杂表头的.多维 ...
- JasperReports® Library | Jaspersoft Community
JasperReport报表导出踩坑实录 - 小卖铺的老爷爷 - 博客园https://www.cnblogs.com/laoyeye/p/7707149.html jasperreport_百度百科 ...
- JAVA实用案例之文件导出(JasperReport踩坑实录)
写在最前面 想想来新公司也快五个月了,恍惚一瞬间. 翻了翻博客,因为太忙,也有将近五个多月没认真总结过了. 正好趁着今天老婆出门团建的机会,记录下最近这段时间遇到的大坑-JasperReport. 六 ...
- JasperReport导出报表8
我们已经看到在前面的章节中,如何打印和查看的JasperReport生成的文档.在这里,我们将看到如何在其他格式,如PDF,HTML和XLS转换或导出这些报告. Facade类net.sf.jaspe ...
- ireport 导出工具类
Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,html 格式 下面是报表导出工具类 Ireport 报表导出 Poi + ireport 导出pdf, ...
- JasperReport原理解析之(一)
1. [加载原始文件]有iReport生成jrxml文件后,由jasperreport包中的类JRXml文件 加载和解析 jrxml文件. 文件解析后生成 JasperDesign对象. Jaspe ...
- Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,htm
Ireport 报表导出 Poi + ireport 导出pdf, doc ,excel ,html 格式 下面是报表导出工具类reportExportUtils 需要导出以上格式的报表 只需要调用本 ...
随机推荐
- worktools-git 工具的使用总结(2)
1.创建分支 git branch son parent //创建分支,是在master 分支的基础上创建 :~/myGit$ git st # On branch master nothing to ...
- php中ajax使用实例
php中ajax使用实例 一.总结 1.多复习:这两段代码都挺简单的,就是需要复习,要多看 2.ajax原理:ajax就是部分更新页面,其实还在的html页面监听到事件后,然后传给服务器进行操作,这里 ...
- Linux 常用解压缩归档命令
linux 常见压缩.归档工具 创建压缩工具 压缩工具 后缀 描述 compress/uncompress .Z 早期工具,现在不常见了 gzip/gunzip .gz 进几年比较火的工具 bzip2 ...
- shell实例浅谈之三产生随机数七种方法
一.问题 Shell下有时须要使用随机数,在此总结产生随机数的方法.计算机产生的的仅仅是"伪随机数".不会产生绝对的随机数(是一种理想随机数).伪随机数在大量重现时也并不一定保持唯 ...
- ED/EP系列1《简单介绍》
电子存折(ED:ElectronicDeposit)一种为持卡人进行消费.取现等交易而设计的支持个人识别码(PIN)保护的金融IC卡应用. 它支持圈存.圈提.消费和取现等交易. 电子钱包(EP:Ele ...
- hibernate hbm.xml配置映射
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Java 学习(17): Java 泛型
Java 泛型 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型. 泛型的本质是参数化类型,也就是说将 ...
- 开源企业IM-免费企业即时通讯-ENTBOOST V2014.177 Windows版本号正式公布
ENTBOOST,VERSION 2014.177 LINUX 版本号公布.主要添加Android安卓手机开发接口.企业IM接口,JQUERY开发接口,PCclient部分BUG修正: 下版本号更新时 ...
- HTTP网络协议(三)
HTTP首部字段有四种类型:通用首部字段,请求首部字段,响应首部字段,实体首部字段. 通用首部字段: 首部字段 说明 Cache-Control 控制缓存的行为 Connection 逐跳首部.连接 ...
- 这一篇sigmoid和softmax的比较,讲的不错
文章: http://blog.csdn.net/u014422406/article/details/52805924 sigmoid函数(也叫逻辑斯谛函数): 引用wiki百科的定义: A lo ...