2011-08-06 57 views
1

我在嘗試讀取Excel 2007電子表格(.xlsx)時遇到問題。我試圖通過使用POI library用Java實現的方法,但是我得到這個錯誤:從JAVA中的Excel表單2007中讀取數據

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException

,這是我的方法:

public void No_rows() throws IOException { 
    File inputWorkbook = new File(inputFile); 
    FileInputStream w = new FileInputStream(inputWorkbook); 
    XSSFWorkbook workbook = new XSSFWorkbook(w); 
    XSSFSheet sheet = workbook.getSheetAt(0); 
    Iterator rows = sheet.rowIterator(); 
    int number=sheet.getLastRowNum(); 
    this.num_rows = number; 
    w.close(); 
} 

回答

0

這是創建和讀取* .XLSX文件。如果出現該錯誤,請包括jaxp--api-1.4.jar

public class Readxlsx { 
    public static void main(String[] args) { 
     FileOutputStream fos = null; 
     FileInputStream fis = null; 
     try 
     { 
      fos = new FileOutputStream(new File("D:\\prac\\sample1.xlsx")); 
      XSSFWorkbook wb = new XSSFWorkbook(); 

      for(int m=0;m<3;m++){ 
       if(m==0) 
       { 
        XSSFSheet sh = wb.createSheet("Sachin"); 
        System.out.println(" Sheet NO:"+m); 

        for (int k = 0; k < 30; k++) { 
         XSSFRow row = sh.createRow((short)k); 
         for (int i = 0; i < 30; i++) { 
          XSSFCell cell = row.createCell((short)i); 
          cell.setCellValue(wb.getSheetName(m)+i); 
         } 
        } 

       } 
       else if(m==1){ 
        XSSFSheet sh1 = wb.createSheet("Dravid"); 
        System.out.println(" Sheet NO:"+m); 

        for (int k = 0; k < 30; k++) { 
         XSSFRow row = sh1.createRow((short)k); 
         for (int i = 0; i < 30; i++) { 
          XSSFCell cell = row.createCell((short)i); 
          cell.setCellValue(wb.getSheetName(m)+i); 
         } 
        } 

       } 
       else 
       { 

        XSSFSheet sh2 = wb.createSheet("Dhoni"); 
        System.out.println(" Sheet NO:"+m); 

        for (int k = 0; k < 30; k++) { 
         XSSFRow row = sh2.createRow((short)k); 
         for (int i = 0; i < 30; i++) { 
          XSSFCell cell = row.createCell((short)i); 
          cell.setCellValue(wb.getSheetName(m)+i); 
         } 
        } 
       } 
      } 

      wb.write(fos); 
      fos.close(); 

      fis= new FileInputStream(new File("D:\\prac\\sample1.xlsx")); 
      XSSFWorkbook workbook = new XSSFWorkbook(fis); 
      XSSFSheet sheet = workbook.getSheetAt(0); 
      java.util.Iterator<org.apache.poi.ss.usermodel.Row> rows = sheet.rowIterator(); 
      int number=sheet.getLastRowNum(); 
      System.out.println(" number of rows"+ number); 

      while (rows.hasNext()) 
      { 
       XSSFRow row = ((XSSFRow) rows.next()); 
       int r=row.getRowNum(); 
       System.out.println(" Row NO:"+r); 
       java.util.Iterator<org.apache.poi.ss.usermodel.Cell> cells = row.cellIterator(); 

       while(cells.hasNext()) { 
        XSSFCell cell = (XSSFCell) cells.next(); 
        String Value=cell.getStringCellValue(); 
        System.out.println(Value); 
       } 
      } 
     } 
     catch(Exception e) { 
      e.printStackTrace(); 
     } 
} 
} 
0

正如@ Michael-O在註釋中提到的那樣,類路徑中缺少XML Beans。 XMLBeans庫可以在http://xmlbeans.apache.org/找到。

0

xml bean library

您必須在您的構建路徑的xmlbeans庫。它通常在你的poi-apache庫的ooxml-lib中。