天气与日历 切换到窄版

 找回密码
 立即注册
中国膜结构网
十大进口膜材评选 十大国产膜材评选 十大膜结构设计评选 十大膜结构公司评选
查看: 7|回复: 0

读取excel表格数据

[复制链接]
  • TA的每日心情
    开心
    昨天 15:23
  • 签到天数: 69 天

    [LV.6]常住居民II

    410

    主题

    167

    回帖

    2704

    积分

    管理员

    积分
    2704
    发表于 2024-6-22 09:46:18 | 显示全部楼层 |阅读模式
    1、获得Workbook,可以解决现在excel中以xls和xlsx两种格式的excel表格,代码如下:


    String path = "C:\test.xls" ;//excel表格的地址字符串
                    String fileType = path.substring(path.lastIndexOf(".") + 1,
                                    path.length());
                    InputStream stream = new FileInputStream(path);
                    Workbook wb = null;
                    if (fileType.equals("xls")) {
                            wb = (Workbook) new HSSFWorkbook(stream);
                    } else if (fileType.equals("xlsx")) {
                            wb = new XSSFWorkbook(stream);
                    } else {
                            wb = null;
                    }




    2、获得Sheet对象


    /**
    * 获得Sheet对象
    */
    sheet = book.getSheetAt(0);

    3、获得第0行标题内容

    Cell cell1, cell2, cell3;//这里只是3列做实验,实际开发中,可根据实际情况写
                    // 获取左上角的单元格
                   
                    Row row =sheet.getRow(0)  ;
                    row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                    cell1 = row.getCell(0);

    4、获得excel中的最后一行的行号数

    int total = sheet.getLastRowNum() ;//获得excel中的最后一行的行号数

    5、获取每一行的单元格

    // 获取每一行的单元格
                            row = sheet.getRow(i) ;

    6、获取每一行每一列的数据

    for (int i = 2; i < total; i++) {
                            // 获取每一行的单元格
                            row = sheet.getRow(i) ;
                            for(int j=0;j<3;j++) {
                                    /**
                                     * POI操作Excel时偶尔会出现Cannot get a text value from a numeric
                                     * cell的异常错误。
                                     * 异常原因:Excel数据Cell有不同的类型,当我们试图从一个数字类型的Cell读取出一个字符串并写入数据库时,
                                     * 就会出现Cannot get a text value from a numeric cell的异常错误。
                                     * 此异常常见于类似如下代码中:row.getCell(0).getStringCellValue();
                                     * 解决办法:先设置Cell的类型,然后就可以把纯数字作为String类型读进来了:
                                     * if(row.getCell(0)!=null){
                                     * row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                                     * stuUser.setPhone(row.getCell(0).getStringCellValue()); }
                                     */
                                    row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
                            }
                            cell1 = row.getCell(0) ;// 第一列
                            cell2 = row.getCell(1);//第二列
                            cell3 = row.getCell(2);// 第三列
                           
                            String one = cell1.getStringCellValue();//第一列数据
                            String two = cell2.getStringCellValue();//第二列数据
                            String three = cell3.getStringCellValue();//第三列数据
                    }




    补充:上面提供的jar包不全,会导致下面两个的异常:



    解决异常的jar包地址:解决下面异常的jar包-excel2003和2007的jar包都有

       

    第一个:

    执行到这一样时:new XSSFWorkbook(stream);
       
    异常:

    Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException
    这个异常时因为缺少jar包,jar包的名字是:xmlbeans-2.3.0.jar
       
    第二个异常:

    异常:  Caused by: java.lang.ClassNotFoundException: org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet 这个是因为,使用POI中的XSSFWorkbook操作excel2007(xlsx)的时候抛出的异常: 解决方案: poi包中默认不支持excel2007,如果需要解析,则需要引入poi-ooxml-schemas-xx.jar包。 引入此包后,可能还会有个异常,原因是找不到dom4j的jar包。 因为poi-ooxml-schemas-xx.jar需要依赖dom4j-xxx.jar包

     

     

     

     

    读取excel表格数据
    中国膜结构网打造全中国最好的膜结构综合平台 ,统一协调膜结构设计,膜结构施工,膜材采购,膜材定制,膜结构预算全方位服务。 中国空间膜结构协会合作单位。
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|手机版|中国膜结构网|中国膜结构协会|进口膜材|国产膜材|ETFE|PVDF|PTFE|设计|施工|安装|车棚|看台|污水池|

    GMT+8, 2024-7-1 05:29 , Processed in 0.059261 second(s), 22 queries .

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.

    快速回复 返回顶部 返回列表