无法使用jasper报告在xls文档中显示图表

时间:2015-11-01 17:51:50

标签: java excel charts jasper-reports

我是jasper reports的新手。我可以通过Jasper报告将数据导出到excel表。当我在iReports中创建一个饼图时,它在预览中显示。当我在jsp文件中编写以下代码以查看JasperViewer中的报告时,它会显示数据和图表。但是当我尝试将该文档(即JasperViewer,它提供了许多以PDF或RTF或XLS格式保存报表的方式)保存为XLS时,然后在控制台中出现错误

net.sf.jasperreports.engine.JRException: The cell cannot be added.

然而,当我从.jrxml文件中删除图表时,我可以将数据导出到excel文件。我还阅读了一些将JR图表作为图像插入excel文档的地方。但是xls文档没有拍摄任何图像。下面是我的JSP文件:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.sql.*,java.util.*,
    net.sf.jasperreports.engine.*,java.io.*,
net.sf.jasperreports.engine.design.*,
net.sf.jasperreports.view.JasperViewer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3309/jasperdb", "root", "root");
FileInputStream fis=new FileInputStream(new File("C://Documents and Settings//james//report1.jrxml"));
JasperReport jasperreport=JasperCompileManager.compileReport(fis);
JasperPrint jasperprint=JasperFillManager.fillReport(jasperreport,null,con); 
      JasperViewer jv=new JasperViewer(jasperprint,false);
      jv.setTitle("test");
      jv.setIconImage(null);
      jv.setVisible(true);
}catch(Exception ex){
    ex.printStackTrace();
}
%>
</body>
</html>

加载jsp时的输出enter image description here

当我点击“保存”按钮并选择xls作为我要保存的报告时,控制台中会出现以下错误:

net.sf.jasperreports.engine.JRException: The cell cannot be added.
    at net.sf.jasperreports.engine.export.JRXlsExporter.exportImage(JRXlsExporter.java:1683)
    at net.sf.jasperreports.engine.export.JRXlsAbstractExporter.exportPage(JRXlsAbstractExporter.java:1047)
    at net.sf.jasperreports.engine.export.JRXlsAbstractExporter.exportReportToStream(JRXlsAbstractExporter.java:831)
    at net.sf.jasperreports.engine.export.JRXlsAbstractExporter.exportReport(JRXlsAbstractExporter.java:678)
    at net.sf.jasperreports.view.save.JRSingleSheetXlsSaveContributor.save(JRSingleSheetXlsSaveContributor.java:123)
    at net.sf.jasperreports.swing.JRViewerToolbar.btnSaveActionPerformed(JRViewerToolbar.java:406)
    at net.sf.jasperreports.swing.JRViewerToolbar$1.actionPerformed(JRViewerToolbar.java:136)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/codec/digest/DigestUtils
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.addPicture(HSSFWorkbook.java:1610)
    at net.sf.jasperreports.engine.export.JRXlsExporter.exportImage(JRXlsExporter.java:1667)
    ... 43 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.codec.digest.DigestUtils
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
    ... 45 more

这是因为图表图像而发生的。指导我如何将图表作为图像导出为xls文档中的其余数据。

1 个答案:

答案 0 :(得分:2)

哦!我真是个傻瓜。最底层的控制台错误是尖叫,告诉Caused by: java.lang.ClassNotFoundException: org.apache.commons.codec.digest.DigestUtils 找不到类的类异常为了解决它我下载了commons-codec-1.4.jar文件并放在web-inf lib文件夹中。现在一切都很好。