我在使用poi jar在excel工作簿中编写一些数据时,我的代码中出现了这些异常:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
at WorkBookDemo.main(WorkBookDemo.java:27)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.UnsupportedFileFormatException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 13 more
我添加了以下罐子:
的log4j-1.2.17
import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
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 WorkBookDemo {
public static void main(String[] args)
{
//Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
//Create a blank sheet
XSSFSheet sheet = workbook.createSheet("Employee Data");
//This data needs to be written (Object[])
Map<String, Object[]> data = new TreeMap<String, Object[]>();
data.put("1", new Object[] {"ID", "NAME", "LASTNAME"});
data.put("2", new Object[] {1, "Amit", "Shukla"});
data.put("3", new Object[] {2, "Lokesh", "Gupta"});
data.put("4", new Object[] {3, "John", "Adwards"});
data.put("5", new Object[] {4, "Brian", "Schultz"});
//Iterate over data and write to sheet
Set<String> keyset = data.keySet();
int rownum = 0;
for (String key : keyset)
{
Row row = sheet.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 Integer)
cell.setCellValue((Integer)obj);
}
}
try
{
//Write the workbook in file system
FileOutputStream out = new FileOutputStream(new File("exps.xlsx"));
workbook.write(out);
out.close();
System.out.println("exps.xlsx written successfully on disk.");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
答案 0 :(得分:2)
你丢失了jar文件。我在我的工作区中运行了代码,并添加了必需的jar来构建路径。它先后工作。
org/apache/poi/UnsupportedFileFormatException is under poi-x.xx-xxx-xx.jar
代码中没有错误是正确的。添加jar来构建路径。
答案 1 :(得分:2)
FileOutputStream out = new FileOutputStream(new File("exps.xlsx"));
因为您显式创建了一个File
对象,并将其传递给FileOutputStream
构造函数。它假定已创建文件"exps.xlsx"
。 [Reference.]
Incase,它不是,你只需在FileOutputStream
构造函数中传递文件的名称。
FileOutputStream out = new FileOutputStream("exps.xlsx");
这将自动创建文件,因为它尚未创建。
此外,我测试了您的给定代码并使用了以下Maven
依赖项并且它有效。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-FINAL</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10</version>
</dependency>
如果你没有使用maven
项目,你可以明确地添加前面提到的jars
版本。
答案 2 :(得分:2)
apachePOI库所需的以下jar列表应该都是相同的版本,如下所示:
org.apache.poi.3.11
org.apache.poi-ooxml.3.11
org.apache.poi-ooxml-schemas.3.11
如果同一个库的上述jar有不同的版本,那么你将获得异常&#34; java.lang.NoClassDefFoundError:org / apache / poi / UnsupportedFileFormatException&#34;
答案 3 :(得分:0)
你也可以检查重复的poi罐子,假设你需要poi-ooxml-3.9。罐。但是在你的lib文件夹中有poi-ooxml-3.11.jar,poi-ooxml-3.9.jar然后删除poi-ooxml-3.11.jar,它应该可以工作。