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

标签 java excel excel-2007 xlsx

我在尝试读取 Excel 2007 电子表格 (.xlsx) 时遇到问题。我正在尝试使用 POI library 在 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();
}

最佳答案

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

关于java - 用JAVA从Excel工作表2007读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6966106/

相关文章:

excel - 自定义订单排序

excel - VBA - 网页抓取无法获取 HTMLElement insideText

excel - VBA Solver 禁用每次迭代后弹出的对话框

PHPexcel如何在浏览器中显示多张数据

java - 从另一个 JavaFX 应用程序中打开另一个应用程序?

扩展参数类/接口(interface)的java基类

vba - Excel VBA 中的 for 循环

java - MapReduce中First Reduce Task作为<Key Value>对的输出值

java - Thread-Safe在java中是什么意思或者什么时候我们叫Thread-Safe?

excel - 在没有 VBA 的情况下从函数反转一维数组输出(或获取最后一个值)?