簡體   English   中英

在JAVA中從Excel sheet 2007讀取數據

[英]Reading data from Excel sheet 2007 in JAVA

嘗試讀取Excel 2007電子表格(.xlsx)時遇到問題。 我正在嘗試通過使用POI庫在JAVA中實現該方法,但出現此錯誤:

線程“主”中的異常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();
}

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

xml bean庫

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

這是為了創建和讀取* .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();
        }
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM