2015-10-06 105 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

    相關問題