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
Related
I have a source src Excel file to read, and a destination dst file to overwrite for edited changes after reading src. With k=true, the following code fails with POIXMLException. With k=false, both files are written.
Exception in thread "main" org.apache.poi.ooxml.POIXMLException:
java.io.EOFException: Unexpected end of ZLIB input stream
at org.apache.poi.ooxml.POIXMLDocument.getProperties(POIXMLDocument.java:146)
at org.apache.poi.ooxml.POIXMLDocument.write(POIXMLDocument.java:225)
at Test.main(Test.java:26)
What should I do to keep intact the 'src' file?, hopefully not changing the ´Worksheet´ and ´Sheet´ classes.
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class Test {
static Workbook wb;
static Sheet sh;
public static void main(String[] args) throws Exception {
Files.copy(Paths.get("src\\src.xlsx"),Paths.get("src\\dst.xlsx"),
StandardCopyOption.REPLACE_EXISTING);
File fl; boolean k=true;
if (k)
fl= new File("src\\src.xlsx"); //Fails
else
fl = new File("src\\dst.xlsx"); //Do Not Fails
wb = WorkbookFactory.create(fl);
sh = wb.getSheetAt(0);
Row row = sh.getRow(1);
row.getCell(1).setCellValue("Test");
OutputStream fos = new FileOutputStream("src\\src.xlsx");
wb.write(fos); // [FAILS]
wb.close();
}
}
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>
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.
When i try to read from excel file using apace poi i get the ClassNotFoundException followed by other errors and i have imported all the necessary jar files in the reference library
by the way i'm still new to coding
heres the code :
import java.io.*;
import java.io.File;
import java.io.FilterInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.*;
import org.apache.poi.*;
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.FormulaEvaluator;
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) throws IOException {
// TODO Auto-generated method stub
FileInputStream F = new FileInputStream("Carbcounting.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(F);
XSSFSheet sheet = wb.getSheetAt(0);
FormulaEvaluator formulaEva = wb.getCreationHelper().createFormulaEvaluator();
for(Row row : sheet){
for(Cell cell : row){
System.out.print(cell.getStringCellValue());
}
}
System.out.println();
}
}
and heres all the errors i'm getting when i try to run it :
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap
at project.Test.main(Test.java:28)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections4.ListValuedMap
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
You need to Add commons-collections4-x.x.jar file in your build path and try it again. It should work.
Get it from here: https://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.0
Also, just adding up:
You are getting this error(NoClassDefFoundError) majorly for two reasons:
Java Virtual Machine is not able to find a particular class at runtime which was available at compile time.
If a class was present during compile time but not available in java classpath during runtime.
So I am receiving error messages as the XSSFWorkbook import appears to be incompatible with the final line. The code as it is gives the error message that package "org.apache.poi.xssf.usermodel.XSSFWorkbook" does not exist. When I remove the import, the last line then gets the error message that I should create an XSSFWorkbook class. I am currently using the Apache 3.7 utilmodel file in order to import the XSSFWorkbook class. Any help would be most appreciated. I am trying to create a program that reads data from an excel file.
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ReadExcel {
private static final String FILE_PATH =
"/Users/riveraaz/Desktop/FantasyPros_2015_Overall.numbers";
public static List getPlayersFromExcel() {
List PlayerList = new ArrayList();
FileInputStream fis = null;
try {
fis = new FileInputStream(FILE_PATH);
Workbook workbook = new XSSFWorkbook(fis);