用JAVA从Excel工作表2007读取数据

发布于 2024-11-28 05:48:41 字数 657 浏览 0 评论 0原文

我在尝试读取 Excel 2007 电子表格 (.xlsx) 时遇到问题。我试图使用 POI 库 在 JAVA 中实现该方法,但出现此错误:

线程“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();
}

I am having a problem when trying to read an Excel 2007 spreadsheet (.xlsx). I am trying to implement the method in JAVA by using the POI library, but I get this error:

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

and this is my method:

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一百个冬季 2024-12-05 05:48:41

这是创建并读取 *.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();
        }
}
}

This is to create and read an *.xlsx file. If you get that error, include the 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();
        }
}
}
倚栏听风 2024-12-05 05:48:41

正如 @Michael-O 在评论中提到的,类路径中缺少 XML Bean。 XMLBeans 库可以在 http://xmlbeans.apache.org/ 找到。

As mentioned by @Michael-O in the comments, XML Beans are missing from the classpath. The XMLBeans library can be found at http://xmlbeans.apache.org/ .

以可爱出名 2024-12-05 05:48:41

xml bean 库

您必须在构建路径中包含 xmlbeans 库。它通常位于 poi-apache 库的 ooxml-lib 中。

xml bean library

You have to include xmlbeans library in your build path. It is usually in your ooxml-lib in your poi-apache library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文