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 需要导出以上格式的报表 只需要调用本 ...
随机推荐
- metadata 和 routing
虽然在刚开始源码概述时把代码分为分布式和数据两部分,但是它们的界限并不明显.之前这几篇可以说是这两部分的衔接.我们在快速接近数据(index)部分.本篇分析一下之前分析cluster遗留下的问题:Me ...
- Linux平台下使用AdventNet ManageEngine OpUtils监控网络
AdventNet ManageEngine OpUtils 是一套系统和网络监视工具,它有Linux/Windows系统平台的免费版和企业版,该软件是一款用于监视诸如路由器,交换机,服务器或者桌面这 ...
- c#中文字符串与byte数组互相转化
因为中文字符串一个字符占两个字节,所以不能用正常的方式与byte之间进行互相转化 中文字符串转成byte[] byte[] ping = Encoding.UTF8.GetBytes("你的 ...
- 创建VG
创建VG smit mkvg Add a Volume Group Add a Scalable Volume Group VOLUME GROUP name ...
- Flask项目之手机端租房网站功能测试(完结)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 目录 一丶注册和登录以及用户退出功能 二丶上传头像功能和修改用户名功能测试 三丶发布房源以及实名认证功能测试 四丶网站房屋搜索功能 ...
- Spider_scrapy
多线程爬虫 进程线程回顾 进程 系统中正在运行的一个应用程序 1个CPU核心1次只能执行1个进程,其他进程处于非运行状态 N个CPU核心可同时执行N个任务 线程 进程中包含的执行单元,1个进程可包含多 ...
- Scala——构造函数
Scala的构造函数分为主构造函数和辅助构造函数. 辅助构造函数 辅助构造函数比较容易理解,它们同C++和Java的构造函数十分类似,只有两处不同: 1.辅助构造函数的名称为this,这主要是考虑到在 ...
- curl如何发起DELETE/PUT请求
curl如何发起DELETE/PUT请求 DELETE: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); PUT: curl_setopt($ch ...
- 1.10 Python基础知识 - 序列:列表
在Python中有很多的组合数据类型,其中包括列表,元组,字符串等数据类型,这些数据类型统称为序列类型,用他们可以处理复杂的数据. 列表,是一组有序元素组合的数据结构.列表是可变的数据类型. 列表采用 ...
- Css 显示删除条目效果
样式设置