我正在尝试使用Apache poi写入excel(.xlsx)文件,我在我的pom.xml文件中包含了apache poi依赖项。但是我在执行中遇到以下异常。
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at test.ExcelWriting.main(ExcelWriting.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.UnsupportedFileFormatException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 13 more
代码和pom.xml指定如下。
我在以下行中获得了异常。
XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);
代码:
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
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 ExcelWriting {
public static void main(String[] args) throws IOException{
File myFile = new File("/home/sabra/workspace/test/src/main/resources/test.xlsx");
FileInputStream fis = new FileInputStream(myFile);
// Finds the workbook instance for XLSX file
XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);
// Return first sheet from the XLSX workbook
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
Map<String, Object[]> data = new HashMap<String, Object[]>();
data.put("7", new Object[] {7d, "Sonya", "75K", "SALES", "Rupert"});
data.put("8", new Object[] {8d, "Kris", "85K", "SALES", "Rupert"});
data.put("9", new Object[] {9d, "Dave", "90K", "SALES", "Rupert"});
// Set to Iterate and add rows into XLS file
Set<String> newRows = data.keySet();
// get the last row number to append new data
int rownum = mySheet.getLastRowNum();
for (String key : newRows) {
// Creating a new Row in existing XLSX sheet
Row row = mySheet.createRow(rownum++);
Object [] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
Cell cell = row.createCell(cellnum++);
if (obj instanceof String) {
cell.setCellValue((String) obj);
} else if (obj instanceof Boolean) {
cell.setCellValue((Boolean) obj);
} else if (obj instanceof Date) {
cell.setCellValue((Date) obj);
} else if (obj instanceof Double) {
cell.setCellValue((Double) obj);
}
}
}
// open an OutputStream to save written data into XLSX file
FileOutputStream os = new FileOutputStream(myFile);
myWorkBook.write(os);
myWorkBook.close();
}
}
波姆:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11-beta2</version>
</dependency>
</dependencies>
</project>
答案 0 :(得分:5)
我认为你错过了一些类“UnsupportedFileFormatException”
尝试将poi版本更改为相同并且不要使用3.11-beta2
您可以在3.12版中使用它们 http://mvnrepository.com/artifact/org.apache.poi
答案 1 :(得分:1)
我使用Jar Libs尝试了你的代码而不是maven(参见项目图片),它运行正常。就像Athi所说的那样,你缺少一些libs,或者你的文件类型不是xlsx。
PS:我刚刚更改了这行代码 来自
File myFile = new File("/home/sabra/workspace/test/src/main/resources/test.xlsx");
到
File myFile = new File("test.xlsx");