- meira
-
产生原因:在生成HTML页面时JRHtmlExporterParameter.IMAGES_URI参数设置不正确,主要是图片的路径问题。由JasperReport生成Html时,那些红叉叉是一个名称为px像素的图片,是内置在jasperreports包中的,在IE显示时以图片的形式显示
解决办法:根据JasperReport的源代码显示,我们只需要以下两个步骤就能解决了
1、在web.xml中添加如下配置:
<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>/servlets/image</url-pattern> </servlet-mapping>
2、在调用程序中增加如下代码:
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "../servlets/image?image=");
好了,经过上面两个步骤。重启服务,刷新页面立马就正常了
完整代码:
/** * 导出html */ private static void exportHtml(JasperPrint jasperPrint,String defaultFilename, HttpServletRequest request, HttpServletResponse response) throws IOException, JRException { response.setContentType("text/html;charset=UTF-8"); JRHtmlExporter exporter = new JRHtmlExporter(); PrintWriter out = response.getWriter(); try { request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out); exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE); exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8"); exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.FALSE); exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "../servlets/image?image="); exporter.exportReport(); } catch (JRException e) { logger.debug(" 生成html文件失败 .... ...."); } }
上面的代码就能很完美的生成html了,页面没有红叉叉,chat图也能正常显示了
- gitcloud
-
问题描述:使用JasperReport生成Html报表后,数据显示正确,但各种线条显示不出来。
产生原因:在生成HTML页面时JRHtmlExporterParameter.IMAGES_URI参数设置不正确,主要是图片的路径问题。由JasperReport生成Html时,那些线条是一个名称为px像素的图片,是内置在jasperreports包中的,在IE显示时以图片的形式显示
解决办法:
1、在web.xml中添加如下配置:
<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>/servlets/image</url-pattern>
</servlet-mapping>
2、在调用程序中增加如下代码:
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath()+"/servlets/image?image=");
全部调用程序如下:
<%@ page contentType="application/pdf;charset=GB2312"%>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.util.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="net.sf.jasperreports.j2ee.servlets.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%
// "/reports/test.jasper"是ireport编译后的报表文件
File reportFile = new File(application.getRealPath("/reports/test.jasper"));
if(!reportFile.exists())
throw new JRRuntimeException("报表绘制失败,找不到报表配置文件!");
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
String url ="jdbc:jtds:sqlserver://127.0.0.1/test";
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(url,"sa", "");
//null为无参数传入
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, conn);
JRHtmlExporter exporter = new JRHtmlExporter();
StringBuffer sbuffer = new StringBuffer();
session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath()+"/servlets/image?image=");
exporter.exportReport();
//如果是pdf输出的话,可以用下面的代码
/*
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
exporter.exportReport();
*/
%>
3、执行代码正常显示
- 寸头二姐
-
没有什么限制吗?例如:文件大小或宽度