2017-02-28 127 views
0

附在下面是用於讀取使用POI作爲一個初學者Excel文件中的代碼需要幫助無法使用Apache POI來讀取Excel拋出NoClassDefFoundError的

package genericReusable; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.util.ArrayList; 

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.xssf.usermodel.XSSFWorkbook; 

public class ExcelReader 
{ 

     public String[] readExcel(String filePath,String fileName,String sheetName) throws IOException 
     { 

        //Create a object of File class to open xlsx file 
         File file = new File(filePath);//new File(filePath+"\\"+fileName); 

        //Create an object of FileInputStream class to read excel file 
         FileInputStream inputStream = new FileInputStream(file); 


         Workbook wrkbk = new XSSFWorkbook(inputStream); 

         //Read sheet inside the workbook by its name 
         Sheet sheet = wrkbk.getSheet(sheetName); 

         //Find number of rows in excel file 
         int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum(); 

         // Array list to Store the Data 
         ArrayList<String> Data_Array = new ArrayList<String>(); 

         //Create a loop over all the rows of excel file to read it 
         for (int i = 1; i < rowCount+1; i++) 
         { 
          Row row = sheet.getRow(i); 

          //Create a loop to print cell values in a row 
          for (int j = 0; j < row.getLastCellNum(); j++) 
          { 

          //Print excel data in console 
          System.out.print(row.getCell(j).getStringCellValue()+"|| "); 

          Data_Array.add(row.getCell(j).getStringCellValue()); 

          } 

          System.out.println(); 

         } 

         return (String[]) Data_Array.toArray(new String[ Data_Array.size() ]); 

     } 




public static void main(String...strings) throws IOException 

{ 


ExcelReader obj = new ExcelReader(); 
String filePath = System.getProperty("user.dir")+"\\src\\genericReusable"; 


obj.readExcel(filePath,"ExportExcel.xlsx","Login"); 

} 
} 

運行代碼顯示下面的錯誤。不知道爲什麼有NoClassDefFoundError的,因爲我已經把文件中的適當位置 需要幫助的傳遞Excel和閱讀使用Apache POI

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject 
at genericReusable.ExcelReader.readExcel(ExcelReader.java:26) 
at genericReusable.ExcelReader.main(ExcelReader.java:73) 
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject 
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) 
... 2 more 
+0

什麼行會拋出錯誤? – mrfreester

+0

@mrfreester更新了結果..仍顯示錯誤 – Abhishek

+0

我在詢問引發錯誤的那一行。例如,是'System.out.println();'拋出錯誤?它是'Workbook wrkbk = new XSSFWorkbook(inputStream);'等...請儘可能多地進行調試,以幫助人們縮小可能出現的問題。謝謝! – mrfreester

回答

1

正如錯誤所暗示的,它是不能夠找到依賴正在項目中使用的類。

您必須在項目中添加XmlBeans依賴項。添加它,錯誤將會消失。您可以從here下載jar

相關問題