can't find apache poi libraries - java

i'm trying for the first time the apache poi library to manage Execel files. I follow this steps to import apache poi: Import Apache POI in Intellij for JAVA.
The code compile without error but when i run it give me this error messages:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject
at Test.main(Test.java:16)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
... 1 more
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Test {
public static void main(String[] args) {
try {
File file = new File("C:\\Users\\Huawei\\IdeaProjects\\untitled\\src\\TEST.xlsx");
FileInputStream fis = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
Iterator<Row> itr = sheet.iterator();
while(itr.hasNext()){
Row row = itr.next();
Iterator<Cell> celliterator = row.cellIterator();
while(celliterator.hasNext()){
Cell cell = celliterator.next();
switch (cell.getCellType()){
case STRING: //field that represents string cell type
System.out.print(cell.getStringCellValue() + "\t\t\t");
break;
case NUMERIC: //field that represents number cell type
System.out.print(cell.getNumericCellValue() + "\t\t\t");
break;
default:
System.out.println("NOT NUMERIC AND NOT STRING");
}
}
}
}catch (Exception e ){
e.printStackTrace();
}
}
}

This seems like a classic Maven Project with a .jar downloaded not installed via mvn command.
If ClassNotFoundException raises in runtime, its because your project can't find the library after compiled.
Is this the case? If so, you must add the .jar via mvn install and declare him in your pom.xml. A better solution it's just adding the maven dependency directly:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>

Related

NoClassDefFoundError in Apache POI/FileOutputStream

I get NoClassDefFoundError error when I try run below code. I checked tons of similar posts, but it didn't helped. What do I wrong? I think, there is something with FileOutputStream but I don't know what.
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Vienna");
File viennaBatch = new File("C:\\Users\\majan\\Desktop\\CitiJanus\\Vienna.xls");
viennaBatch.createNewFile();
Row row = sheet.createRow(1);
Cell cell = row.createCell(1);
cell.setCellValue("a");
FileOutputStream fileOut
= new FileOutputStream("C:\\Users\\majan\\Desktop\\CitiJanus\\Vienna.xls");
workbook.write(fileOut);
fileOut.close();
}
}```
error:
```Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/math3/util/ArithmeticUtils
at org.apache.poi.poifs.property.RootProperty.setSize(RootProperty.java:59)
at org.apache.poi.poifs.property.DirectoryProperty.<init>(DirectoryProperty.java:52)
at org.apache.poi.poifs.property.RootProperty.<init>(RootProperty.java:31)
at org.apache.poi.poifs.property.PropertyTable.<init>(PropertyTable.java:58)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:99)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:121)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1357)
at Main.main(Main.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.util.ArithmeticUtils
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 8 more```
Quote from the POI page:
Apache commons-math3 and commons-compress were added as a dependency
in POI 4.0.0. Zaxxer SparseBitSet was added as a dependency in POI
4.1.2
The exception you're getting indicates that commons-math3 is missing in the classpath.

Poi api XSSFWorkbook sheet cast EXCEPTION

package apsel5;
import java.io.FileInputStream;
import java.util.Iterator;
import org.apache.poi.sl.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class A {
public static void main(String[] args) throws Exception{
FileInputStream f = new FileInputStream("D:\\LeadSuite.xlsx");
XSSFWorkbook wbks = new XSSFWorkbook(f);
Sheet s = (Sheet) wbks.getSheet("TestSteps");
Iterator itr = s.iterator();
while(itr.hasNext()){
Row rowitr = (Row)itr.next();
Iterator cellitr = rowitr.cellIterator();
while(cellitr.hasNext()){
Cell cell1= (Cell)cellitr.next();
switch(cell1.getCellType()){
case Cell.CELL_TYPE_STRING:
System.out.println(cell1.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.println(cell1.getNumericCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cell1.getBooleanCellValue());
break;
}
}
}
}
}
I get an exception after running the above code:
Exception in thread "main" java.lang.ClassCastException:
org.apache.poi.xssf.usermodel.XSSFSheet cannot be cast to
org.apache.poi.sl.usermodel.Sheet
You imported the wrong version of Sheet. Simply turn
import org.apache.poi.sl.usermodel.Sheet;
into
import org.apache.poi.ss.usermodel.Sheet;
that represents the Excel worksheet, and your code should work. No cast should be needed for Sheet, Row and Cell.
If you look at the XSSFSheet class definition, you can see that it implements org.apache.poi.ss.usermodel.Sheet. On the other hand, the org.apache.poi.sl.usermodel.Sheet interface is related with PowerPoint; in fact, according to the Javadoc, it's the
Common parent interface for Slides, Notes and Masters
So .ss. = Excel (XSSF), and .sl. = PowerPoint (XSLF). The fact that these two interfaces have the same name may actually be misleading.

Getting error at Iterator while reading from excel sheet

Even though I have imported
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
I am getting error at part
Iterator<Cell> cellIterator=row.cellIterator();
Here is my full code
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
//import org.apache.poi.hssf.record.formula.functions.Cell;
//import org.apache.poi.hssf.record.formula.functions.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class UploadExcel {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream(new File("C:\\Documents and Settings\\admin\\Desktop\\imp data\\howtodoinjava_demo.xlsx"));
//Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator=row.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
//Check the cell type and format accordingly
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
}
}
System.out.println("");
}
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
The error which i am getting is:
Type mismatch: cannot convert from java.util.Iterator<org.apache.poi.ss.usermodel.Cell> to java.util.Iterator<org.apache.poi.hssf.record.formula.functions.Cell>
Could you please help me with this.
You also need this dependency for xssf:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.7</version>
</dependency>
If you don't get compile errors now it means you have this dependency from somewhere else, likely your RiverBoat project exposes an older/incompatible version. So either you need both poi dependencies in this pom, or none (as both may be exposed by RiverBoat).

Read excel file in Java with NetBeans

I need to read an excel file. I wrote this code but it has an error.
Here is my code:
package javaapplication9;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.*;
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
try{
//File fileName = new File("C://sadegh//test.xlsx");
InputStream inp = new FileInputStream("C://sadegh//test.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);
System.out.println(cell.toString());
}catch(java.lang.NullPointerException e5){
//e5.notify();
}catch (org.apache.poi.openxml4j.exceptions.InvalidFormatException e3) {
e3.printStackTrace();
}
catch (IOException e1) {
e1.printStackTrace();
}
}
}
This is the error as shown in the StackTrace:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at org.apache.poi.openxml4j.opc.Package.<clinit>(Package.java:63)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:62)
at javaapplication9.Main.main(Main.java:35)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at sun.misc.Launcher$ExtClassLoader.findClass(Launcher.java:229)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 3 more
Java Result: 1
What are the causes of the error?
Here are my libs imported. I'm using:
xbean.jar
xbean_xpath.jar
xmlbeans-qname.jar
dom4j-1.6.1.jar
poi-ooxml-3.8-20120326.jar
poi_ooxml-schemas-3.8-20120326.jar
poi-3.8-20120326.jar
You need the log4 jar as lib in the classpath
Your Apache POI requires Log4J 1.2.13
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.13</version>
</dependency>
You can download it here: http://central.maven.org/maven2/log4j/log4j/1.2.13/log4j-1.2.13.jar

Cannot find class on runtime even after including the jar files in the classpath

I am trying to use the apache poi api. I have downloaded the jar libraries. This is the code that I have written. The name of the file is Main.java.
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
File inputFile = new File(".\test.xlsx");
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(inputFile));
HSSFSheet sheet = workbook.getSheetAt(0);
Cell cell;
Row row;
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()){
row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()){
cell = cellIterator.next();
System.out.println(cell.getStringCellValue());
}
}
}
}
This what I code that I am running the compile the file.
javac -cp "./poi-3.12-20150511.jar;./poi-ooxml-3.12-20150511.jar;./poi-ooxml-schemas-3.12-20150511.jar" Main.java
I am not getting any errors while I am compiling. But when I am trying to run it I am getting a class not found exception for HSSFWorkbook. What am I doing wrong.
PS - All the jar files and my java code are in the same folder.
Try this:
java -cp "poi-3.12-20150511.jar;poi-ooxml-3.12-20150511.jar;poi-ooxml-schemas-3.12-20150511.jar;." Main

Categories