Trouble Compiling Apache POI 5.2.2 Code - Factory.newInstance() methods - apache-poi

Apache POI newbie.
I am trying to use Apache POI 5.2.2 to generate footnotes in a Word document.
There are some useful SO posts on this topic:
Need to add a footnote in between texts and the next text should be appeared in the same line not in the next line in Apache poi
Need to insert the text at the end of the page but the table is still continuing in the next page
However, when I try to run those examples, I get very consistent errors on these 3 Factory.newInstance() methods:
CTFtnEdn.Factory.newInstance();
CTStyle.Factory.newInstance();
CTDecimalNumber.Factory.newInstance();
The compiler says: "error: incompatible types: XmlObject cannot be converted to" either CTFtnEdn, CTStyle or CTDecimalNumber, as applicable.
Here is the full compilation error for one of these, but all 3 are similar:
Footnotes.java:68: error: incompatible types: XmlObject cannot be converted to CTFtnEdn
CTFtnEdn ctfInstance = CTFtnEdn.Factory.newInstance();
poi-ooxml-full-5.2.2.jar is included in the compilation classpath.
I assume I have not included one or more jar files needed to get this working.
Any ideas how to get this working?

I believe I had conflicting jar files in the classpath. Removing one has fixed the issue. Axel Richter, thanks for helping me identify that.

Related

SLF4J version mismatch with GATE stanford parser

I am trying to use stanford parser in GATE and this is the error I am getting.
The requested version 1.5.6 by your slf4j binding is not compatible with [1.6, 1.7]
Now, I have downloaded a file a slf4j-api-1.5.6 and have put it in same location as I had my slf4j-api-1.7 file. (ivy2\cache\org.slf4j\slf4j-api\jars) This path is also set as a classpath.
I also deleted slf-4j-1.7 but it keeps reoccurring itself every time I start GATE.
How should I resolve this issue? Any suggestions will be helpful.
Thanks!

May I use jxls and apache poi together?

I'm making an application to analize some data and the result must be presented in excel files. In that sense I started to use Apache POI (3.11). Due to some reports consumes a lot of time and memory to be reproduce, I made an investigation and I found jxls, after some test I thought was the solution. But now I found a problem: can´t work both frameworks together.
I have to update Apache POI from 3.11 to 3.14, in order to work with jxls-2.3.0
I made an extra package in order to make my tests with jxls, not problem
I try to migrated one of my classes from Apache POI to jxls, and a I got this error: java.lang.IllegalStateException: Cannot load XLS transformer. Please make sure a Transformer implementation is in classpath. This is the code of my method:
private void prepareNewReport(File excelFile) {
List perforaciones = makePerforacionReport
.makePerforacionData(escenario);
try (InputStream is = ReportePerforacionTotalDialog.class
.getResourceAsStream("PerforacionTotal_template.xls")){
try (OutputStream os = new FileOutputStream(excelFile)) {
Context context = new Context();
context.putVar("perforaciones", perforaciones);
JxlsHelper.getInstance().processTemplate(is, os, context);
LOGGER.logger.log(Level.INFO, "Archivo de perfortacion generado con éxito");
}
} catch (IOException e) {
LOGGER.logger.log(Level.SEVERE, "Problemas buscando el archivo", e);
}
}
How could be this possible?. In the same project I have my test class, just another package and its working fine. As you can see it´s not so much different from the example in the jxls page and the imports are the same.
But even worst, when I tried to make clean & build of my project, then I got this other error:
java.lang.RuntimeException: com.sun.tools.javac.code.Symbol$CompletionFailure: class file for org.openxmlformats.schemas.officeDocument.x2006.docPropsVTypes.CTArray not found
I looked at every library that I importede in order to work with jxls and apache poi, and that´s rigth, that class is not there. Just to see if there a conflict among these two framewoks, I eliminated from the class path all libraries needed to use jxls. Clean & build again, and not problem, I have my .jar file to send to my customer, but incomplete.
I could try to replace all classes that use Apache POI, but that means a lot of work, since POI is used in my project to read excel files with data many times and to write another many files to excel. I planned to use jxls in order to take advantage of use templates.
I will apreciate any help or suggestion.
For the first error, it would appear that the JXLS transformer for Apache POI is missing in your classpath when running the application. Check the JXLS getting started info here: http://jxls.sourceforge.net/getting_started.html
As it is explained in Transformers section (see Main Concepts)) Jxls core module does not depend on any specific Java-Excel library and works with Excel exclusively through a predefined interface. Currently Jxls supplies two implementations of this interface in separate modules based on the well-known Apache POI and Java Excel API libraries.
If you're using maven, be sure to include in your pom.xml the jxls-poi dependency listed on the JXLS getting started page:
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>1.0.9</version>
</dependency>
For the second issue, org.openxmlformats.schemas.officeDocument.x2006.docPropsVTypes.CTArray is not in the apache POI ooxml schemas jar files for either 3.11 (poi-ooxml-schemas-3.11-20141221.jar) or 3.14 (poi-ooxml-schemas-3.14-20160307.jar). POI uses a stripped down set of ooxml schema classes, you will need to get the ooxml schemas complete jar from http://central.maven.org/maven2/org/apache/poi/ooxml-schemas/1.3/ or if you're using maven (or another build tool), get the dependency for your build from https://mvnrepository.com/artifact/org.apache.poi/ooxml-schemas/1.3
e.g for maven:
<!-- https://mvnrepository.com/artifact/org.apache.poi/ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.3</version>
</dependency>
Be sure to remove the poi-ooxml-schemas dependency from your maven pom.xml so that ooxml-schemas above takes precedence instead.

HSSFWorkbook cannot be resolved to a type

I am getting this compilation error
HSSFWorkbook cannot be resolved to a type
when i am using this class in Selenium webdriver.I have added the respective jar, poi 3.9 jar, but still unable to resolve this compilation error.
Hi please update your apache poi to The latest stable release is Apache POI 3.14 from https://poi.apache.org/download.html and then import the Apache POI jars form
jars inside poi-3.14
jars inside lib
Look for "poi-3.17.jar"!!!
Download from "https://poi.apache.org/download.html".
Click the one Binary Distribution -> poi-bin-3.17-20170915.tar.gz
Unzip the file download and look for this "poi-3.17.jar".
Problem solved and errors disappeared.
Hope this answer your question!!!
The message indicates that the class could not be found on the classpath of your application. Did you make sure that the POI jar is added?

XSSFWorkbook poi: adding image IOUtils toByteArray unfined error

To insert image to excel using POI:XSSF
I am using maven poi dependency:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8</version>
</dependency>
AND code as :
InputStream my_banner_image = new FileInputStream("input.png");
byte[] bytes = IOUtils.toByteArray(my_banner_image);
int my_picture_id = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
I am getting these errors:
1) The method toByteArray(InputStream) is undefined for the type IOUtils
2) PICTURE_TYPE_PNG cannot be resolved or is not a field
Any help would be appriciated. Thanks.
Promoting a comment to an answer:
The method you want to use is very much present in Apache POI 3.11, and you can see full details about it in the POI Javadocs.
As detailed on the POI Components page, defining a Maven dependency on poi-ooxml will pull in the main poi component jar, which is where the IOUtils class lives, so that bit is fine
What you have in this case (based on comments) is a second, older copy of POI on your classpath. You need to remove this older POI jar (or POI jars), in common with most Java projects Apache POI will only work properly if all of the POI jars are from the same version, and no old ones are present.
Because it's a fairly common problem - lots of frameworks ship old copies of POI for example - there's a POI FAQ entry on this very thing. If you can't find the old jar by hand, you can use the code given there to work out where the old jar is to remove it.
Also, one other thing to bear in mind - many many projects provide a class called IOUtils - make sure the one you have imported into your code is org.apache.poi.util.IOUtils and not something else!

Error compiling JasperReport 4.1.3 created from iReport 4.1.3 with Groovy 1.8.1

I'm on Windows 7 64-bit, using JDK 1.6.0_29, groovy 1.8.1, and JasperReports/iReport 4.1.3.
I have the following jars from the JasperReports distribution in my CLASSPATH:
commons-beanutils-1.8.0.jar, commons-collections-2.1.1.jar, commons-digester-1.7.jar, commons-logging-1.0.4.jar, jasperreports-4.1.3.jar, poi-3.7-20101029.jar
I have also tried adding all the jars in the ${JASPER_HOME}/lib distribution in case I was missing some important jar.
The jasper report is to be generated by a groovy script. I have created a very simple report with iReport that contains only some static text in the title. I am able to compile and view the report from within iReport.
When I try and compile the report from the script:
def jasperReport = JasperCompileManager.compileReport("filename")
I get the following error:
Caught: java.lang.AbstractMethodError
java.lang.AbstractMethodError
at net.sf.jasperreports.compilers.JRGroovyCompiler.compileUnits(JRGroovyCompiler.java:96)
at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:188)
at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:212)
at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:145)
at net.sf.jasperreports.engine.JasperCompileManager$compileReport.call(Unknown Source)
at wis_validate_environment.run(wis_validate_environment.groovy:58)
Is there possibly some conflict between the groovy that JasperReports is using (JRGroovyCompiler? from the stacktrace) and the groovy that I am using to compile the report?
There were a couple of things I had to change in order to resolve this issue.
Creating a report using the Report Wizard and the default settings creates the language="groovy" parameter in the jasperReport tag. When I remove language parameter, I no longer get the AbstractMethodError mentioned above.
I also needed to add the jdt-compiler-3.1.1.jar in my classpath.
I found the problem was explained here:
http://www.mail-archive.com/user#ofbiz.apache.org/msg23404.html
There seems to be a problem with the versions of one of the dependent libraries that our project uses that conflicts with groovy's.
In short use groovy-all.jar or make sure are your dependancies match those of the version of groovy you are using.

Resources