2011-08-06 13 views
1

Excel 2007スプレッドシート(​​.xlsx)を読み込もうとすると問題が発生します。私はPOI libraryを使用してJAVAのメソッドを実装しようとしていますが、私はこのエラーを取得:Excelシート2007からのデータをJavaで読み取る

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/にあります。あなたのビルド・パスでのXMLBeansライブラリを含める必要が

0

xml bean library

。通常poi-apacheライブラリのooxml-libにあります。

関連する問題