excel表格导入数据库数据存在则更新不存在添加

    public void excelToDB() throws ParseException {
        String datapath = this.getParameter("datapath");
        String filePath = AppUtil.getAppAbsolutePath()+datapath;
        Workbook rwb = null;
        
        try {
            // 创建输入流
            InputStream stream = new FileInputStream(filePath);
            // 获取Excel文件对象
            rwb = Workbook.getWorkbook(stream);
        } catch (Exception e) {
            e.printStackTrace();    
        }//导入文件
        Sheet rs = rwb.getSheet(0);//
        int rows = rs.getRows();//得到所有的行
        for(int i=1;i<rows;i++) {
            Map<String,Object> map = null;
                //如果busreqno,entno为空则添加
            String busregno = rs.getCell(0,i).getContents();
            String entno = rs.getCell(2,i).getContents();
            map = yksptBuildManager.getEntityDao().findObjectBySql("sql查询语句",busregno);
            if(map == null && !"".equals(entno) && entno!= null) {
                map = yksptBuildManager.getEntityDao().findObjectBySql("sql查询语句",entno);
            }
            if(map == null) {
                map = new HashMap<String,Object>();
            }
            //没查到就插入数据库
            所导入实体类 entity = new 所导入的实体类();
            boolean flag = false;
            if(map.size() != 0) {
                entity = yksptBuildManager.getEntityDao().getById((String)map.get("所导入表id"));
                flag = true;
            }
                
            entity.setBname(rs.getCell(1,i).getContents());
            entity.setBaddress(rs.getCell(2,i).getContents());
            entity.setOnesitekey(rs.getCell(3,i).getContents());
            entity.setTwositekey(rs.getCell(5,i).getContents());
            //根据下标获取excle表中经度纬度,取逗号前0后1
            String s=rs.getCell(6,i).getContents();
            String[] ss=s.split(",");
            if(!"".equals(ss) && ss!= null) {
                entity.setCoordinatesx (ss[0]);
                entity.setCoordinatesy(ss[1]);
            }
            if(!"".equals(rs.getCell(7,i).getContents())&& rs.getCell(7,i).getContents()!=null) {//根据excle表内数据匹配后台相对应状态,001,,002,003~~存入库内
                entity.setBstreet(this.sysDictionaryManager.findDictNameByGroupkeyAndHvalue("ykspt.street",rs.getCell(7,i).getContents()));
            }
            entity.setManage(rs.getCell(8,i).getContents());
            entity.setBtype(rs.getCell(9,i).getContents());
            entity.setWorkstation(rs.getCell(10,i).getContents());
            entity.setBuiltuparea(rs.getCell(12,i).getContents());
            entity.setUpperarea(rs.getCell(13,i).getContents());
            entity.setSparearea(rs.getCell(14,i).getContents());
            entity.setPropertyunit(rs.getCell(15,i).getContents());
            
            if(flag) {
                //更新数据库
                this.yksptBuildManager.update(entity);
            }else {
                //插入数据
                this.yksptBuildManager.save(entity);
            }
            System.out.println(i);
        }
    }

猜你喜欢

转载自www.cnblogs.com/yanchaohui/p/10070361.html