2017-04-21 145 views
-1

Here i am trying to read the excel file and want to edit it if required.讀取Excel使用Apache POI

//code here 
 try 
     { 
      FileInputStream file = new FileInputStream(new File("howtodoinjava_demo.xlsx")); 

      //Create Workbook instance holding reference to .xlsx file 
      XSSFWorkbook workbook = new XSSFWorkbook(file); //ERROR HERE 
      //Get first/desired sheet from the workbook 
      XSSFSheet sheet = workbook.getSheetAt(0); 

      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()) 
       { 
       //code here 
       } 
      } 
     } 

Blockquote here i am getting error

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap at com.Edetails.Dao.ReadExcel.main(ReadExcel.java:27) Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections4.ListValuedMap 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) ... 1 more

+0

另請請幫我如何編輯單元格中的值爲我讀取的同一個文件。 – Rajeevan

回答

0

我用的是不同版本的POI罐子。而且我忘了將dom4j.jar包含到項目中。

添加該jar後,它工作正常。請參考link獲取最新的poi jar。