Exception in thread "main" java.lang.NoSuchFieldError: Factory question - java

I'm trying to test a xml to excel converter in Java and I have the following exception
Exception in thread "main" java.lang.NoSuchFieldError: Factory
at org.apache.poi.xssf.usermodel.XSSFWorkbook.onWorkbookCreate(XSSFWorkbook.java:475)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:232)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:226)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:214)
at xmlToExcel.Converter.initXls(Converter.java:135)
at xmlToExcel.Converter.getAndReadXml(Converter.java:60)
at xmlToExcel.Converter.main(Converter.java:36)
The exception refers to the this line:
workbook = new XSSFWorkbook();
And here are my current jars
How can I solve this? Are there any conflicts in the jars?
EDIT 1: Here is the code for the method which initialises the POI workbook and writes the header row
workbook = new XSSFWorkbook();
CellStyle style = workbook.createCellStyle();
Font boldFont = workbook.createFont();
boldFont.setBold(true);
style.setFont(boldFont);
style.setAlignment(HorizontalAlignment.CENTER);
Sheet sheet = workbook.createSheet();
rowNum = 0;
Row row = sheet.createRow(rowNum++);
Cell cell = row.createCell(SUBSTANCE_NAME_COLUMN);
cell.setCellValue("Substance name");
cell.setCellStyle(style);
The exception references the first line, the creation of the workbook.

While using apache poi never mix jars from different versions. See faq-N10204.
So when used apache poi version 5.2.1 all apache poi jars needs to be from that version. So in your case apache poi 5.2.1 cannot be used together with poi-ooxml-schemas-4.1.2.jar. It needs to be used with either poi-ooxml-lite-5.2.1.jar or poi-ooxml-full-5.2.1.jar.
To avoid conflicts, furthermore all jars from not matching versions should be excluded from class path while running apache poi code.
So in your case using apache poi 5.2.1 there should not be any poi-ooxml-schemas-4.1.2.jar in class path additional to either poi-ooxml-lite-5.2.1.jar or poi-ooxml-full-5.2.1.jar. Also ooxml-schemas-1.4.jar should not be in class path while using apache poi 5. The ooxml-schemas-1.4.jar was the former version of poi-ooxml-full-5.*.jar and also cannot be used together with apache poi 5.

Related

How to create pivot table in SXSSF sheet using apache poi library in java?

XSSFSheet sheet = workbook.getXSSFWorkbook().getSheetAt(0);
XSSFSheet pivot_sheet = workbook.getXSSFWorkbook().createSheet("Sheet1");
XSSFPivotTable pivotTable = pivot_sheet.createPivotTable(new AreaReference("B10:AJ24"), new CellReference("B2"),sheet);
I am getting following error at above line -
java.lang.NullPointerException at
org.apache.poi.xssf.usermodel.XSSFPivotCacheDefinition.createCacheFields(XSSFPivotCacheDefinition.java:145)
at
org.apache.poi.xssf.usermodel.XSSFSheet.createPivotTable(XSSFSheet.java:4065)
I didn't find solution for this using apache poi.
Workaround that I used is mentioned as follows :
Create SXSSFWorkbook as below
SXSSFWorkbook workbook = new SXSSFWorkbook(new XSSFWorkbook(new FileInputStream("xyz.xlsm")));
workbook.removeSheetAt(0);
xyz.xlsm is macro enabled excel which holds VBA code for generating pivot
Write in workbook as usual
Pivots will get generated in excel on opening it after download

Apache poi cloneSheet partly copying comments (comment is there but no drawing)?

I am using Apache POI 4.1 and the cloneSheet() method on top of an XSSFWorkbook to clone a sheet. Unfortunately, comments seem to be properly cloned but without the underlying drawings.
Any code to remove comments from cells on newSheet result in a null pointer exception due to non-existing drawing object instances.
No comments are visible on the new sheet after opening the resulting file in Excel.
final Workbook wb = sourceSheet.getWorkbook();
// duplicate base sheet
Sheet newSheet = wb.cloneSheet(wb.getSheetIndex(sourceSheet));

java apache syntax poi 3.11 vs 3.16

I have developed a program in which POI 3.11 is used. After upgrading to POI 3.16, the error ..depricated .. is shown for some instances (for example, HSSFCellStyle.BORDER_THIN).
Despite intensive search, I am unable to find the correct new syntax for the function ...setBorderLeft(HSSFCellStyle.BORDER_THIN).
Here is the section of the code:
try {FileInputStream file = new FileInputStream(new File(eP));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFFont cF = workbook.createFont();
HSSFCellStyle cS=workbook.createCellStyle();
cS.setBorderLeft(HSSFCellStyle.BORDER_THIN);
How can I migrate the code to POI 3.16?
Greetings from Aachen
According to the documentation of Apache Poi, since the version 3.15, the method setBorderLeft(....) consumes newly the instance of the BorderStyle enum.
I suggest you to try the following:
HSSFCellStyle cS=workbook.createCellStyle();
cS.setBorderLeft(BorderStyle.THIN);

POI is not recognised

Im using struts application. I want to work on reports. So going to try POI. I got this jar file poi-3.6-20091214.jar and put them in web-inf/lib folder.When i try using them in class file poi is not recognised. Im trying below...
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //..
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sample sheet"); //Create a new row in current sheet
Row row = sheet.createRow(0); //Create a new cell in current row
Cell cell = row.createCell(0); //Set value to new value
cell.setCellValue("Blahblah");
But import itself wouldnt work.Any idea?
Have you added into the eclipse lib path?
another thing: use poi 3.8, is more recent. Why start with an old release?
I think its HSSFRow & HSSFCell and not Row and Cell. These are included in the jar.
One point to consider using POI is that the same code will not be able to handle both .xls and .xlsx file formats.
missed this: it will be XSSF if you are using .xlsx files and HSSF for .xls file format.

How to open .xlsx files with POI SS?

I am trying to open .xlsx files with POI SS with this code (taken from http://poi.apache.org/spreadsheet/quick-guide.html#ReadWriteWorkbook):
InputStream inp = new FileInputStream("workbook.xls");
//InputStream inp = new FileInputStream("workbook.xlsx");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
if (cell == null)
cell = row.createCell(3);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("a test");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
and I get this error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
I add the xbean.jar to my library and to my run-time libraries.
how can I resolve this exception?
Thanks !
First: Fix the Exception
There are two solutions:
As Gagravarr already mentioned: you need dom4j to fix your exception.
As Jon already mentioned: you have to update your dependencies, so you don't need dom4j anymore.
If you're using Maven, you can add the necessary dependencies with: (Maybe check for newer versions at: Maven Repository: org.apache.poi)
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
Then: Open the File
If you've fixed the exception, you can open your file.xlsx file with the following code:
String path = "Relative/Path/To/Your/File/file.xlsx";
File file = new File(path);
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
// Use your sheet ...
Further tips
Like Gagravarr, I also recommend to use a file instead of a file input stream.
If you want to open a certain sheet you can use workbook.getSheet(String name);
If you don't know the relative path to your file according to your project, you can easily check it with System.out.println("Relative path: " + System.getProperty("user.dir"));
Regards, winklerrr
I haven't analyzed your error message, but after seeing the code, I see that there is something not correct.
The Wookbook is not work with *.xlsx (Office 2007 and after) files. So you have to use the XSSFWorkbook. You may want to change the wb initialization to:
XSSFWorkbook wb = (XSSFWorkbook) WorkbookFactory.create(inp);
This can help you.
InputStream is = new FileInputStream(pathOfYourXlsxFile);
XSSFWorkbook workbook = new XSSFWorkbook(is);
//Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
For your Exception you have to put dom4j.jar to your classpath.You can find it here
You need dom4j, that's what the exception is telling you
You might want to look at the components page on the POI website which lists the dependencies
Also, since you have a File, open directly with that, don't go via an InputStream. There's a section in the Apache POI FAQ on that, basically using a file is quicker and lower memory than buffering the whole thing via a stream!
I realize the thread is old, but it led me today to the answer I needed for the same problem.
The docs on the page suggested above (i.e include dom4j.jar) say that's no longer necessary:
"The OOXML jars used to require DOM4J, but the code has now been changed to use JAXP and no additional dom4j jars are required."
I found that including the following resolved the problem:
poi-3.11-20141221.jar
poi-ooxml-3.11-20141221.jar
poi-ooxml-schemas-3.11-20141221.jar
xmlbeans-2.6.0.jar

Categories