POI:上传EXCEL解析

来源:互联网 发布:怎么做好淘宝店铺 编辑:程序博客网 时间:2024/05/29 05:53
public String upload()throws Exception{
        POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream(userUploadFile));//根据流来创建workbook
        HSSFWorkbook wb=new HSSFWorkbook(fs);
        HSSFSheet hssfSheet=wb.getSheetAt(0);  // 获取第一个Sheet页
        if(hssfSheet!=null){
            for(int rowNum=1;rowNum<=hssfSheet.getLastRowNum();rowNum++){
                HSSFRow hssfRow=hssfSheet.getRow(rowNum);
                if(hssfRow==null){
                    continue;
                }
                User user=new User();
                user.setName(ExcelUtil.formatCell(hssfRow.getCell(0)));
                user.setPhone(ExcelUtil.formatCell(hssfRow.getCell(1)));
                user.setEmail(ExcelUtil.formatCell(hssfRow.getCell(2)));
                user.setQq(ExcelUtil.formatCell(hssfRow.getCell(3)));
                Connection con=null;
                try{
                    con=dbUtil.getCon();
                    userDao.userAdd(con, user);
                }catch(Exception e){
                    e.printStackTrace();
                }finally{
                    dbUtil.closeCon(con);
                }
            }
        }

    }


public static String formatCell(HSSFCell hssfCell){
        if(hssfCell==null){
            return "";
        }else{
            if(hssfCell.getCellType()==HSSFCell.CELL_TYPE_BOOLEAN){
                return String.valueOf(hssfCell.getBooleanCellValue());
            }else if(hssfCell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){
                return String.valueOf(hssfCell.getNumericCellValue());
            }else{
                return String.valueOf(hssfCell.getStringCellValue());
            }
        }
    }

0 0