用Java从.xlsx文件中读取数据

时间:2014-08-25 11:24:14

标签: java apache-poi

我正在尝试从.xlsx文件中提取数据,因为我使用的是POI-bin-3.11-beta2-2.jar,此处创建了我的FileInputStream,但问题出在工作簿中。我认为我的xssf引用无法到达文件位置。

public class DemoExcel
{
    public static void main(String[] args) throws Exception
    {
        //File excel = new File("C:\\Users\\aditya.lodha\\Desktop\\test.xlsx");
        FileInputStream fis = null;

        try{
            fis = new FileInputStream(new File("C:\\Users\\aditya.lodha\\Desktop\\test.xlsx"));
            System.out.println("file found");
        }
        catch(FileNotFoundException e){
            System.out.println("file not found");
            e.printStackTrace();
        }
        System.out.println(fis.toString());
        XSSFWorkbook wb = new XSSFWorkbook(fis);
        //HSSFWorkbook wb = new HSSFWorkbook(fis);
        System.out.println(wb.toString());
        XSSFSheet sh = wb.getSheet("tabledata");
        //HSSFSheet sh = wb.getSheet("XYZ");
        //System.out.println(sh.toString());
        int rowNum = sh.getLastRowNum()+1;
        System.out.println(rowNum);
        int colNum = sh.getRow(0).getLastCellNum();
        System.out.println(colNum);
        String data[][] = new String [rowNum][colNum];
        for(int i=0;i<rowNum;i++)
        {
            XSSFRow row = sh.getRow(i);
            for(int j=0;j<colNum;j++)
            {
                XSSFCell col = row.getCell(j);
                String value = celltoString(col);
                data[i][j] = value;
            }
        }

        for(int i=0;i<rowNum;i++)
        {
            for(int j=0;j<colNum;j++)
            System.out.println(data[i][j]);

        }
    }
    private static String celltoString(XSSFCell col)
    {
        int type;
        Object result;
        type = col.getCellType();
        switch (type)
        {
            case 0:
            result = col.getNumericCellValue();
            break;

            case 1:
            result = col.getStringCellValue();
            break;

            default:
            System.out.println(type);
            throw new RuntimeException("Runtime Exception");
        }
        return result.toString();
    }
}

我得到的错误

  

线程中的异常&#34; main&#34; java.lang.NoClassDefFoundError:   组织/阿帕奇/的xmlbeans / XmlException
        在demo.DemoExcel.main(DemoExcel.java:31)
      引起:java.lang.ClassNotFoundException:org.apache.xmlbeans.XmlException
        在java.net.URLClassLoader $ 1.run(未知来源)
        at java.security.AccessController.doPrivileged(Native Method)
        在java.net.URLClassLoader.findClass(未知来源)
        在java.lang.ClassLoader.loadClass(未知来源)
        at sun.misc.Launcher $ AppClassLoader.loadClass(Unknown Source)
        在java.lang.ClassLoader.loadClass(未知来源)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
        ......还有1个

1 个答案:

答案 0 :(得分:2)

您可能需要将XML bean依赖项添加到类路径中。

获取jar(http://poi.apache.org/download.html),转到Project,属性,Java构建路径,添加外部jar并选择.jar文件,或者只是将其放在WEB-INF / libs上,然后类加载器将读取它

jar的名称类似于xmlbeans-(version).jar

相关问题