java.lang.NoSuchMethodError: org.apache.poi.hssf.usermodel.HSSFSheet.setColumnWidth(II)V
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30069316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.NoSuchMethodError: org.apache.poi.hssf.usermodel.HSSFSheet.setColumnWidth(II)V
提问by Madhu Nali
I am trying to generate Jasper Excel report.I am getting the exception like
我正在尝试生成 Jasper Excel 报告。我收到了这样的异常
java.lang.NoSuchMethodError: org.apache.poi.hssf.usermodel.HSSFSheet.setColumnWidth(II)V
at net.sf.jasperreports.engine.export.JRXlsExporter.setColumnWidth(JRXlsExporter.java:212)
at net.sf.jasperreports.engine.export.JRXlsAbstractExporter.setColumnWidths(JRXlsAbstractExporter.java:654)
at net.sf.jasperreports.engine.export.JRXlsAbstractExporter.exportPage(JRXlsAbstractExporter.java:527)
at net.sf.jasperreports.engine.export.JRXlsAbstractExporter.exportReportToStream(JRXlsAbstractExporter.java:423)
at net.sf.jasperreports.engine.export.JRXlsAbstractExporter.exportReport(JRXlsAbstractExporter.java:207)
at com.pagesolutions.controller.ReportingController.doGet(ReportingController.java:177)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
My Java code
我的Java代码
if(formatType.matches("XLS")){
String jrxml = "/home/madhu/report1.jrxml";
list = (List<Reports>) session.getAttribute("customersList");
//InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(jrxml);
try {
//jasperReport = JasperCompileManager.compileReport(jrxml);
//jasperDesign = JRXmlLoader.load(jrxml);
jasperReport = JasperCompileManager.compileReport(jrxml);
JRDataSource datasource = new JRBeanCollectionDataSource(list, true);
jasperPrint = JasperFillManager.fillReport(jasperReport, null, datasource);
String output="/home/madhu/reports/report.xls";
//JasperExportManager.exportReportToPdfFile(jasperPrint, output);
JRXlsExporter exporterXLS = new JRXlsExporter();
exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporterXLS.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, output);
exporterXLS.exportReport();
ServletOutputStream servletOutputStream = response.getOutputStream();
String fileName = "report.xls";
FileInputStream fileToDownload = new FileInputStream(output);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename="+ fileName);
ByteArrayOutputStream output1 = new ByteArrayOutputStream();
int readBytes = 0;
byte[] buffer = new byte[10000];
while ((readBytes = fileToDownload.read(buffer, 0, 10000)) != -1) {
servletOutputStream.write(buffer, 0, readBytes);
}
output1.flush();
output1.close();
fileToDownload.close();
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I already added poi-3.2-FINAL.jar file in my lib but still getting the above exception
我已经在我的库中添加了 poi-3.2-FINAL.jar 文件,但仍然出现上述异常
Can any one help me please....
任何人都可以帮助我吗....
回答by kaqqao
If the project builds but you get this error at runtime, it can mean only one thing: you're building and running with different library versions. The version you built against has this method while the one you ran with doesn't. It could have also happened that you have multiple versions on the classpath and the wrong one is loaded first. I don't know how you build your project, but if you're using some kind of dependency manager, analyse the structure for duplicate POI versions. If you're not using any dependency manager... well... make sure manually that libraries match at build- and runtime, but you really should switch to a build tool that does this for you so that Jasper can automatically drag in the correct version it depends on.
如果项目已构建但您在运行时遇到此错误,则可能仅意味着一件事:您正在使用不同的库版本构建和运行。您构建的版本具有此方法,而您运行的版本则没有。也可能发生在类路径上有多个版本并且首先加载了错误的版本。我不知道你如何构建你的项目,但如果你使用某种依赖管理器,分析重复 POI 版本的结构。如果您没有使用任何依赖项管理器...好吧...手动确保库在构建和运行时匹配,但您确实应该切换到为您执行此操作的构建工具,以便 Jasper 可以自动拖入正确的版本取决于它。
Important note:
Since Jasper already depends on POI, there's no reason for your project to declare that dependency again. It's only asking for trouble of the variety you're seeing now. You just declare your dependency to Jasper and let your dependency manager drag in the correct version of POI. Somehow, fundamental misunderstanding of dependency management is common even among senior developers.
重要说明:
由于 Jasper 已经依赖于 POI,因此您的项目没有理由再次声明该依赖项。它只是自找麻烦,你现在看到的品种。您只需向 Jasper 声明您的依赖项,然后让您的依赖项管理器拖入正确版本的 POI。不知何故,即使在高级开发人员中,对依赖管理的基本误解也很常见。
If, on the other hand, another library you depend on also drags in a different version of POI, you need to either find compatible versions of both third-party libraries (so that they need the same POI), or explicitly? exclude POI from one of them, cross your fingers and hope for the best.
另一方面,如果您依赖的另一个库也拖入了不同版本的 POI,您需要找到两个第三方库的兼容版本(以便它们需要相同的 POI),或者明确地?从其中一个排除 POI,交叉手指并希望最好。
回答by ScottM
I just fixed this error on my application. Upgrading to the latest version of POI (3.15 as of this writing) was the WRONG thing to do.
我刚刚在我的应用程序上修复了这个错误。升级到最新版本的 POI(撰写本文时为 3.15)是错误的做法。
As of Jasperreports 6.3.1, it requires Apache POI 3.10.1. I found this out by looking at the Jasperreports repository POM file, located here: http://repo2.maven.org/maven2/net/sf/jasperreports/jasperreports/6.3.1/jasperreports-6.3.1.pom
从 Jasperreports 6.3.1 开始,它需要 Apache POI 3.10.1。我通过查看位于此处的 Jasperreports 存储库 POM 文件发现了这一点:http://repo2.maven.org/maven2/net/sf/jasperreports/jasperreports/6.3.1/jasperreports-6.3.1.pom
If you are using a different version of Jasper just change to your version in the URL and check the pom file. Search it for POI and it will tell you what version is required.
如果您使用的是不同版本的 Jasper,只需在 URL 中更改为您的版本并检查 pom 文件。搜索 POI,它会告诉您需要什么版本。
As soon as I downgraded from POI 3.15 to 3.10.1 my errors went away and I got a lovely .xls file for my efforts. I had been stuck on this for a while and it feels good to have it finally working!
一旦我从 POI 3.15 降级到 3.10.1,我的错误就消失了,我得到了一个可爱的 .xls 文件。我已经坚持了一段时间,让它终于工作感觉很好!
回答by Bheem Singh
I have changed jar with 'poi-3.11.jar' and 'jxls-reader-1.0.6.jar'
我用'poi-3.11.jar'和'jxls-reader-1.0.6.jar'改变了jar
<dependency>
<groupId>net.sf.jxls</groupId>
<artifactId>jxls-core</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>net.sf.jxls</groupId>
<artifactId>jxls-reader</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>
and added dependency `3.11`
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version>
</dependency>
回答by Tagir Valeev
Update your Apache POI version to the latest one (the latest stable version is 3.11). Version 3.2 is very old. Current version containsthis setColumnWidth(int, int) method which was absent in 3.2 version
将您的 Apache POI 版本更新到最新版本(最新的稳定版本是 3.11)。3.2 版本很旧了。当前版本包含此 setColumnWidth(int, int) 方法,该方法在 3.2 版本中不存在