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 需要导出以上格式的报表 只需要调用本 ...
随机推荐
- 基于TC技术的网络流量控制实战
本文转载在:http://server.it168.com/a2010/0426/878/000000878406.shtml 基于TC技术的网络流量控制实战 650) this.width=650; ...
- Java学习笔记七 常用API对象三
一.泛型:简单说就是对对象类型进行限定的技术 public class GenericDemo { public static void main(String[] args){ /*泛型作为1.5版 ...
- ASP.NET MVC 4 (十一) Bundles和显示模式--asp.net mvc中 @Scripts.Render("~/bundles/jquery")是什么意思? 在布局文件中使用Scripts.Render()输出脚本包,Styles.Render()输出风格包:
ASP.NET MVC 4 (十一) Bundles和显示模式 ASP.NET MVC 4 引入的js打包压缩功能.打包压缩jquery目录下的文件,在布局文件中使用Scripts.Render()输 ...
- InstallShield详细制作说明(四)
十.编译打包
- Vue 消息无缝滚动
vue实现消息向上无缝滚动效果 <ul class="new-list" :class="{anim:animate}" @mouseenter=&quo ...
- JS match方法的返回数据的探究
match方法是JS的字符串方法,详细说明可以看MDN的说明. 如果正则表达式匹配成功的话,match方法会返回一个数组,而数组里的数据有两种形式,对应着匹配方式:全局匹配与非全局匹配. 1. 全局匹 ...
- Spring.net的Demo项目,了解什么是控制反转
Spring这个思想,已经推出很多年了. 刚开始的时候,首先是在Java里面提出,后来也推出了.net的版本. Spring里面最主要的就是控制反转(IOC)和依赖注入(DI)这两个概念. 网上很多教 ...
- hashlib —— Python 的 md5 和 sha1 加密
python的md5和sha1加密 0. md5 与 sha1 MD5 的全称是 Message-Digest Algorithm 5(信息-摘要算法).128 位长度.目前 MD5 是一种不可逆算法 ...
- Linux下读写寄存器
arm裸机下读写寄存器很容易,各个寄存器和内存的地址是单一地址空间,他们是用相同的指令进行读写操作的.而在linux下就要复杂很多,因为linux支持多个体系架构的CPU.比如arm和x86就不一样, ...
- 软件——protel 的pcb电路图制作
近期一直在学习PCB板的绘制.