问题描述
当我使用 poi jar 在 excel 工作簿中写入一些数据时,我的代码中出现了这些异常:
I am getting these exceptions in my code while i m writing some data in excel workbook using poi jars:
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
我添加了以下 jars:
I added following jars:
- xmlbeans-2.4.0
- poi-ooxml-schemas-3.11
- poi-3.11
- commons-logging-1.1
- dom4j-1.6.1
log4j-1.2.17
- xmlbeans-2.4.0
- poi-ooxml-schemas-3.11
- poi-3.11
- commons-logging-1.1
- dom4j-1.6.1
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();
}
}
}
推荐答案
FileOutputStream out = new FileOutputStream(new File("exps.xlsx"));
因为您显式创建了一个 File
对象,并将其传递给 FileOutputStream
构造函数.它假定文件 "exps.xlsx"
已经创建.[参考]
Since you are explicitly creating a File
object, and passing the same to FileOutputStream
constructor. It assumes that the file "exps.xlsx"
is already created. [Reference.]
如果不是,您只需在 FileOutputStream
构造函数中传递文件名.
Incase, it is not, you simply pass the name of the file in FileOutputStream
constructor.
FileOutputStream out = new FileOutputStream("exps.xlsx");
这将自动创建文件,以防它尚未创建.
This will automatically create the file, incase it is already not created.
此外,我测试了您给定的代码并使用了以下 Maven
依赖项并且它起作用了.
Moreover, I tested your given code and used the following Maven
dependency and it worked.
<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
.
Incase you are not using a maven
project, you can explicitly add the aforementioned jars
of the aforementioned versions.
这篇关于使用excel工作簿时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!