Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoSuchMethodError: org.apache.poi.hssf.usermodel.HSSFSheet.setColumnWidth(II)V

I am trying to generate Jasper Excel report.I am getting the exception like

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

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

Can any one help me please....

like image 338
Madhu Nali Avatar asked May 06 '15 06:05

Madhu Nali


2 Answers

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.

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.

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.

like image 190
kaqqao Avatar answered Oct 16 '22 11:10

kaqqao


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.

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

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.

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!

like image 34
ScottM Avatar answered Oct 16 '22 10:10

ScottM