2010-04-20 60 views
16

我必須在java.I中讀取xls文件使用poi-3.6來讀取Eclipse中的xls文件。但是我得到這個錯誤「線程「main」中的異常java.lang.NoClassDefFoundError:org/apache/xmlbeans/XmlException在ReadExcel2.main(ReadExcel2.java:38)「。異常在線程「主」java.lang.NoClassDefFoundError:org/apache/xmlbeans/XmlException

我已經添加下列罐 1)POI-3.6-20091214.jar 2)POI-的contrib-3.6-20091214.jar 3)POI-例子-3.6-20091214.jar 4)POI-ooxml- 3.6-20091214.jar 5)POI-OOXML-架構 - 3.6-20091214.jar 6)POI暫存器-3.6-20091214.jar

下面是一個即時通訊使用的代碼:

import org.apache.poi.ss.usermodel.Workbook; 
import org.apache.poi.ss.usermodel.Sheet; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
import org.apache.poi.xssf.usermodel.XSSFCell; 
import org.apache.poi.xssf.usermodel.XSSFRow; 

import java.io.FileInputStream; 
import java.io.IOException; 
import java.util.Iterator; 
import java.util.List; 
import java.util.ArrayList; 
public class ReadExcel { 

    public static void main(String[] args) throws Exception { 
     // 
     // An excel file name. You can create a file name with a full path 
     // information. 
     // 
     String filename = "C:\\myExcel.xl"; 
     // 
     // Create an ArrayList to store the data read from excel sheet. 
     // 
     List sheetData = new ArrayList(); 

     FileInputStream fis = null; 
     try { 
      // 
      // Create a FileInputStream that will be use to read the excel file. 
      // 
      fis = new FileInputStream(filename); 

      // 
      // Create an excel workbook from the file system. 
      // 
      // HSSFWorkbook workbook = new HSSFWorkbook(fis); 
      Workbook workbook = new XSSFWorkbook(fis); 

      // 
      // Get the first sheet on the workbook. 
      // 
      Sheet sheet = workbook.getSheetAt(0); 

      // 
      // When we have a sheet object in hand we can iterator on each 
      // sheet's rows and on each row's cells. We store the data read 
      // on an ArrayList so that we can printed the content of the excel 
      // to the console. 
      // 
      Iterator rows = sheet.rowIterator(); 
      while (rows.hasNext()) { 
       Row row = (XSSFRow) rows.next(); 
       Iterator cells = row.cellIterator(); 

       List data = new ArrayList(); 
       while (cells.hasNext()) { 
        Cell cell = (XSSFCell) cells.next(); 
        data.add(cell); 
       } 

       sheetData.add(data); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (fis != null) { 
       fis.close(); 
      } 
     } 

     showExelData(sheetData); 
    } 
    private static void showExelData(List sheetData) { 
     // 
     // Iterates the data and print it out to the console. 
     // 
     for (int i = 0; i < sheetData.size(); i++) { 
      List list = (List) sheetData.get(i); 
      for (int j = 0; j < list.size(); j++) { 
       Cell cell = (XSSFCell) list.get(j); 
       System.out.print(cell.getRichStringCellValue().getString()); 
       if (j < list.size() - 1) { 
        System.out.print(", "); 
       } 
      } 
      System.out.println(""); 
     } 
    } 
} 

請幫忙。 感謝預期, 關心, Dheeraj!

回答

33

你在classpath需要xmlbeans

NoClassDefFoundError意味着:

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

所以,下次你得到這樣的異常時,它意味着一些第三方庫需要其他第三方庫。然後使用谷歌(或任何其他手段)來查找這是哪個庫。此外,大多數圖書館在他們的文檔和/或發行版中清楚地陳述了它們的依賴性。

+0

感謝Bozho ...它的工作:) – 2010-04-20 09:19:42

1

對Apache的POI 3.16同樣的錯誤。添加了以下來自Apache POI /ooxml-lib/xmlbeans-2.6.0的jar以及下一個關於要修復的集合/lib/commons-collections4-4.1.jar的異常。

相關問題