Unable to create Instance of XSSFWorkbook using poi-3.9 - java

Need to change the old HSSFWorkbook based code XSSFWorkbook
Towards the task, i have added poi-3.9, poi-ooxml-schemas-3.9, poi-ooxml-3.9 jars to my build path and changed the code as suggested in
http://poi.apache.org/spreadsheet/converting.html
at XSSFWorkbook wb = new XSSFWorkbook() the flow control comes out of the flow at this line.
I try debugging the file, and observed that it was not able to create an instance for XSSFWorkbook.
But after reading from below url,
http://howtodoinjava.com/2013/06/19/readingwriting-excel-files-in-java-poi-tutorial/
i have added xmlbeans-2.3.0 jar, should i also add dom4j.jar also for this?
but still the same issue, control comes out from the line creation of instance for XSSFWorkbook. can any one please help me on this?

The issue is solved, after adding the dependent jar dom4j. Consolidating the details of jars needed for Apache POI 3.9 for XLSX format files to export are as follows,
Poi-,
poi-ooxml,
poi-ooxml-schems,
dom4j

Related

Resolve unreadable content message in Word with Docx4J v. 3.3.3

We are processing a Word template that was created with Word 365 (Version 2202 Build 16.0.14931.20648) in Docx4J. Once the file was modified by our Java application, Word will show an error message when we open the document. The message will state that the file contains unreadable content and that Word needs to repair it. This works fine and the document will eventually open, however, the message is annoying.
I assume that the error message is related to a namespace issue (see this question). That issue was resolved with docx4j v. 8.2.9, which defines the missing namespaces properly.
However, I'm stuck with docx4j 3.3.3 and cannot update. The fix seems rather simple in the GitHub commit, so I wonder, if there is any way to resolve the issue myself. All I have in my code, is a WordprocessingMLPackage object. Can I add or append namespace definitions to that object or it's sub-properties somehow?
You can't add the namespace definitions to the WordprocessingMLPackage object.
You'll need to get the source code for 3.3.3 from https://github.com/plutext/docx4j/tree/docx4j-3.3.3 then copy the new NamespacePrefixMappings content into it, then build it. You can then deploy this new jar file.
If you wanted to try to avoid compiling the source code, you'd have 2 alternatives to try (since docx4j doesn't implement the stategy pattern there). I don't expect these to work!
One is to replace the relevant classes at runtime. For this, see How to replace classes in a running application in java ?
The other is to replace the relevant classes in your jar file, which is just a zip file.
You'd need to get the new class from docx4j 8.2.9.
Please note that sometimes, there are also changes to ContentTypeManager and ContentTypes; see for example https://github.com/plutext/docx4j/commit/d4d02d3fa6e7bf98f35d1f0520e62eb8aef06cba
That commit introduces new parts, and you'll run into problems if you update ContentTypeManager without those.
So you might be tempted to try to replace NamespacePrefixMappings in your existing jar.
But the interfaces it implements changed at https://github.com/plutext/docx4j/commit/65fb843a26b5893200a1824c04c826db2db7940c#diff-70242e2f5ec56be77fe15322526f4530b02e8eafdcb9ae16b60b2220f62e0632
See further https://github.com/plutext/docx4j/commits/VERSION_8_3_8/docx4j-core/src/main/java/org/docx4j/jaxb/NamespacePrefixMappings.java so that is going to cause you problems.
The upshot is you'll need to get the source code for 3.3.3 from https://github.com/plutext/docx4j/tree/docx4j-3.3.3 then copy the new NamespacePrefixMappings content into it (ie except the interface changes), then build it. If you do this, you may as well just deploy your new jar file.

Corrupt Excel Maven

I have some problems with Maven, Excel and poi package.
I access to an excel file thanks to the code :
Workbook workbook = WorkbookFactory.create(new File("src/main/resources/file.xlsx"));
Sheet sheet = workbook.getSheet(sheetName);
This code works correctly and I can read data inside later in my code.
Instead of a "new File(..)", I have to use this code below to access resources in dev mode and once the jar is built.
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
String path = classLoader.getResource(fileName).toURI().getPath();
The given path is in "target/classes" and Maven do a "copy" of this file into folder "myproject/target/classes" of the current project(perfect so).
However, the xslx file copied by Maven is corrupted and neither by using Excel software, I can't access to its content. The original file size is 500Kb, the copied file size is more than 1Mb. (All other files img,txt.. are well copied excepted the xslx files)
I done lots of searches, I could find some answers like :
FileInputStream vs ClassPathResource vs getResourceAsStream and file integrity
. I tried all solutions I could find but impossible to solve mine and I always get the same error :
InvalidOperationException: Could not open the specified zip entry source stream
Or
java.io.FileNotFoundException: file.xlsx
From the same way of classLoader, I can access to my json, txt and image files.
Someone has answer on this issue ?
Why Maven doubles the size of the xlsx files and why they are corrupted ?
Any solution to solve that ?
I need help

Apache POI org.apache.poi.ss.formula.FormulaParseException for shiftrows function call.

I am using Apache POI to read and write Excel files for both xls and xlsx formats.
If the code processes folloiwng line for a file written by POI/my code , it doesn't throw an exception but in case of a file written from Excel by user, I get
org.apache.poi.ss.formula.FormulaParseException: Specified named range 'LOCAL_YEAR_FORMAT' does not exist in the current workbook.
The Exception is fired at:
postWB.getSheet(postSheet.getSheetName()).shiftRows(i, postSheet.getPhysicalNumberOfRows(), 1);
As it turns out from some of the questions here, it can be of jar compatibilities or due to bug. I have changed all jars for latest poi library and dependencies.
Any workaround to shiftrows without finding this exception.

Read excel fie in java using poi

I am struggling to read excel file in a dynamic web project I am developing. I could easily read it in a java application by following the same steps.
What I did?
I added these to build path
xmlbeans-2.6.0.jar
poi-3.13-20150939
poi-examples
poi-excelant
poi-ooxml
poi-ooxml-schemas
poi-scratchpad
Code:
FileInputStream input_document = new FileInputStream(new File("file_location"));
XSSFWorkbook my_xls_workbook = new XSSFWorkbook(input_document);
XSSFSheet my_worksheet = my_xls_workbook.getSheetAt(0);
XSSFCell cell=null;
for(int i=0;i<463;i++){
cell = my_worksheet.getRow(i).getCell(0);
words[i]=cell.getStringCellValue();
System.out.println(words[i]);
}
my_xls_workbook.close();
The Error
SEVERE: StandardWrapper.Throwable
java.lang.NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook
org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet vhealth.GetMed
java.lang.ClassNotFoundException: org.apache.poi.xssf.usermodel.XSSFWorkbook
It is pretty long but this is the gist of it
The Question
I want to know how to solve this and as I mentioned it works in another similar java file after following the same steps. I want to integrate it in the web app. I've also tried calling the routine which imports the database in Java from the WebApp(which uses tomcat if that helps) still no luck.
You need to add jar files not only in the build path. You need obviously to have the same files in your war module in the WEB-INF/lib folder.
P.S. I think you don't need poi-examples jar.

how do i confirm fixed tomcat servlet?

I'm studying Tomcat architecture. I wanted to fix org.apache.jasper.servlet.JspServlet and confirm its result.
For example i add the following code in JspServlet.java init() method.
FileWriter test = new FileWriter("c:\\worktest.txt");
test.write("test!!");
test.close();
After edit code, compile it and repackage jasper.jar file. And then I replace old jasper with new thing. of course restart tomcat.
I think that adding code will work and write text file in C drive. But result doesn't exist.
I've already check .jsp file, but it doesn't have any problem. because of creating JAVA and CLASS file.
There's something wrong with my way?

Categories

Resources