2015-10-06 109 views
0

我想读取使用poi apache库的excel文件。我尝试了不同类型的代码,但仍然得到了与我所有代码相同的错误。我不知道为什么会出现这个错误。试图读取使用poi apache库的excel文件

你可以从这个链接下载POI阿帕奇库: https://poi.apache.org/download.html

这里是我的代码来读取Excel文件:

import java.io.File; 
import java.io.FileInputStream; 
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; 

/** 
* 
* @author Pacer 
*/ 
public class ReadExcelDemo 
{ 
    public static void main(String[] args) 
    { 
     try 
     { 
      System.out.println("Working Directory = " + System.getProperty("user.dir")); 
      FileInputStream file = new FileInputStream(new File("book.xlsx")); 

      //Create Workbook instance holding reference to .xlsx file 
      XSSFWorkbook workbook = new XSSFWorkbook(file); 

      //Get first/desired sheet from the workbook 
      XSSFSheet sheet = workbook.getSheetAt(0); 

      //Iterate through each rows one by one 
      Iterator<Row> rowIterator = sheet.iterator(); 
      while (rowIterator.hasNext()) 
      { 
       Row row = rowIterator.next(); 
       //For each row, iterate through all the columns 
       Iterator<Cell> cellIterator = row.cellIterator(); 

       while (cellIterator.hasNext()) 
       { 
        Cell cell = cellIterator.next(); 
        //Check the cell type and format accordingly 
        switch (cell.getCellType()) 
        { 
         case Cell.CELL_TYPE_NUMERIC: 
          System.out.print(cell.getNumericCellValue() + "t"); 
          break; 
         case Cell.CELL_TYPE_STRING: 
          System.out.print(cell.getStringCellValue() + "t"); 
          break; 
        } 
       } 
       System.out.println(""); 
      } 
      file.close(); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

这里是我得到的错误:

Working Directory = E:\NetBeansProjects\Project24\CoverageCodetool 

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException 
    at coveragecodetool.ReadExcelDemo.main(ReadExcelDemo.java:30) 
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    ... 1 more 
Java Result: 1 

请帮忙!

回答

0

您在类路径中缺少xmlbeans apache库。

将xmlbeans添加到您的类路径中,一切都将工作。

该库本身可以下载here

解决NoClassDefFoundException的一般算法如下:

  1. 搜索对于使用在异常提到的类中的库。我更喜欢this service
  2. 将库添加到您的类路径中
  3. 尝试运行代码并查看问题是否仍然存在。从步骤一个
+0

  • 重复它说组织/阿帕奇/的xmlbeans/XmlException,这是什么意思? – Mitesh

  • +0

    编辑答案 – WeMakeSoftware

    +0

    好的。我添加了import org.apache.xmlbeans.XmlException;正如所建议的,我得到了一些错误: 线程“主”的异常java.lang.NoClassDefFoundError:org/openxmlformats/schemas/drawingml/x2006/main/ThemeDocument – Mitesh

    相关问题