Apache POI - 如何为xlsx创建工作簿

时间:2014-03-31 03:26:38

标签: java excel apache-poi

使用xls时,我可以很好地创建工作簿,但是在使用xlsx时,我似乎无法使其工作。

我正在使用poi-ooxml.jar并尝试使用命令

new XSSFWorkbook();

但是我每次都会收到错误信息,有什么想法吗?

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook
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$000(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 java.lang.ClassLoader.loadClassInternal(Unknown Source)
at Testing2.main(Testing2.java:20)

Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook

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 java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 13 more

3 个答案:

答案 0 :(得分:1)

Apache POI Dependency and Component。你可能会失踪:

对于poi-ooxml,你需要它的依赖

  1. POI,
  2. POI-OOXML-模式
  3. dom4j
  4. POI-OOXML版本-yyyymmdd.jar
  5. 虽然与您的问题无关,但请尝试搜索stacktrace:

    Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook
    

    从长远来看,这将对您有所帮助。

答案 1 :(得分:1)

您需要添加额外的2个罐子才能使POI适用于(.xlsx)Excel文件。

1.首先,您需要导入所有jar poi-3.9

2.将xmlbeans2.3.0.jardom4j-1.6.jar添加到您的类路径中。这两个jar是用于处理POI库中的.xlsx Excel文件的依赖jar。

Maven项目添加以下依赖

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version> 
</dependency>

所以编程创建exls表

 package com.loknath.lab;

import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
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.ss.usermodel.WorkbookFactory;


public class ReadWriteXL
{
 public static void main(String[] args) throws InvalidFormatException, IOException{

System.out.println("Write data to an Excel Sheet");
FileOutputStream fos=new FileOutputStream("D:/temp/1.xlsx");
HSSFWorkbook workBook = new HSSFWorkbook();
HSSFSheet spreadSheet = workBook.createSheet("email");
HSSFRow row;
HSSFCell cell;
for(int i=0;i<arr.size();i++){
row = spreadSheet.createRow((short) i);
cell = row.createCell(i);

cell.setCellValue("string value added");
}

workBook.write(fos);

}
}}

答案 2 :(得分:0)

我认为你需要从http://poi.apache.org/download.html

下载二进制发行版

将jar添加到您的库中,它应该解决错误

一定要添加所有的罐子而不仅仅是poi-ooxml