此例为初次参与企业项目做的,代码粗糙勿见怪:

/**
     * 从EXCEL导入核心企业信息到数据库
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "importWhitelistExcel")
    public ModelAndView readExcel(
            @RequestParam(value="excel",required=false) MultipartFile file
            ) {
        ModelAndView mv = this.getModelAndView();
        PageData pd = this.getPageData();
        
        Whitelist whiteList = new Whitelist();
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;}
        if (null != file && !file.isEmpty()) {
            String filePath = PathUtil.getClasspath() + Const.FILE_PATH_FILE;                    //文件上传路径
            String dateStr = DateUtils.getSdfTimes();
            String fileName = "Whitelist_"+Jurisdiction.getUsername()+"_"+dateStr;            
            fileName =  FileUpload.fileUp(file, filePath, fileName);                              //执行上传
            
            //执行读EXCEL操作,读出的数据导入List 2:从第3行开始;1:从第B列开始;0:第0个sheet
            String errmsg=null;
            //Matcher matcher = null;
            List<PageData> readList=new ArrayList<PageData>();
            List<PageData> errorList=new ArrayList<PageData>();
            
            
            try{
                List<PageData> excelPdList = (List)ObjectExcelRead.readExcel(filePath, fileName, 2, 1, 0, "#.####");
            
                /*数据库写入操作======================================*/
                /*
                 * 白名单信息必输字段:
                 * 核心企业名称,项目公司名称
                 * (为非必输)
                 * 校验:
                 */
                
                for(PageData ca:excelPdList){
                    // 忽略序号
                    PageData readPd = new PageData();
                    PageData tipPd = new PageData();
                    
                    String coreManuName = StringUtils.trimToEmpty(ca.getString("var1")); //核心企业名称
                    String companyName = StringUtils.trimToEmpty(ca.getString("var2")); //项目公司名称
                    String coreManuId = null;
                    //int index = excelPdList.indexOf(ca);
                    
                    // 数据校验开始
                    
                    if(StringUtils.isEmpty(coreManuName))
                    {
                        tipPd.put("coreManuName", coreManuName);
                        tipPd.put("companyName", companyName);
                        if(StringUtils.isEmpty(companyName)){
                            
//                            tipPd.put("tip", "核心企业名称和项目公司名称不能为空");
                            
                            continue;
                        }else{
                            tipPd.put("tip", "核心企业名称不能为空");
                        }
                        errorList.add(tipPd);
//                        errmsg="核心企业名称不能为空";
//                        break;
                    }else if(StringUtils.isEmpty(companyName))
                    {
                        tipPd.put("coreManuName", coreManuName);
                        tipPd.put("companyName", companyName);
                        tipPd.put("tip", "项目公司名称不能为空");
                        errorList.add(tipPd);
//                        errmsg="核心企业["+coreManuName+"]项目公司名称不能为空";
//                        break;
                    }
//                    CoreCompany c = coreCompanyService.findByCompanyName(coreManuName);
//                    System.out.println(coreManuName);
//                    System.out.println(c);
                    if(!StringUtils.isEmpty(coreManuName)){
                        
                        CoreCompany coreManu = coreCompanyService.findByCompanyName(coreManuName);
                        
                        if(coreManu == null) {
                            tipPd.put("coreManuName", coreManuName);
                            tipPd.put("companyName", companyName);
                            tipPd.put("tip", "系统找不到核心企业名称,核心企业名称错误");
                            errorList.add(tipPd);
                        }else {
                            coreManuId = coreManu.getId();
                            whiteList = whitelistService.findByWhitelistName(coreManuId, companyName);
                            if(whiteList != null) {//判断导入表中是否存在相同数据
                                tipPd.put("coreManuName", coreManuName);
                                tipPd.put("companyName", companyName);
                                tipPd.put("tip", "此记录已录入系统");
                                errorList.add(tipPd);
//                                errmsg = "核心企业["+coreManuName+"]和项目公司["+companyName+"在系统中已存在,请在导入表删除后重新导入";
//                                break;
                            }
                        }
                        
                    }
                    
                    readPd.put("coreManuId", coreManuId);   //核心企业编号
                    readPd.put("companyName", companyName); //项目公司名称
                    readPd.put("status", "EBST0101");
                    readPd.put("createUser", Jurisdiction.getUsername());
                    readPd.put("createTime", DateUtils.getTime());
                    readPd.put("lasUpdUser", Jurisdiction.getUsername());
                    readPd.put("lasUpdTime", DateUtils.getTime());
                    readPd.put("Id", this.get32UUID()); //ID
                    
                    if(!readList.isEmpty()) {
                        
                        for(PageData read : readList) {
                            
                            if(read.containsValue(companyName) && read.containsValue(coreManuId)){
                                //判断导入表中是否有相同项目公司
                                tipPd.put("coreManuName", coreManuName);
                                tipPd.put("companyName", companyName);
                                tipPd.put("tip", "此记录在导入表中重复");
                                errorList.add(tipPd);
//                                errmsg = "导入表里核心企业["+coreManuName+"]和项目公司["+companyName+"]在表中重复,请修改后导入";
//                                break;
                            }
                        }
                    }
                    readList.add(readPd);
                }
            }catch(Exception ex){
                logger.error("处理Excel文件出错", ex);
                errmsg="处理Excel文件出错,"+ex.getMessage();
            }
            
            
            if(errmsg==null && readList.size()==0){
                errmsg="未检测到符合条件的数据";
            }
            
            if(errmsg!=null || errorList.size()>0){
                if(errmsg==null){
                    errmsg="数据导入失败,请核对以下数据";
                }
                mv.addObject("errmsg", errmsg);
                mv.addObject("tipList", errorList);
                mv.setViewName("business/whitelist/whitelist_upload");
                return mv;
            }
            
//            if(errmsg!=null){
//                mv.addObject("errmsg", errmsg);
//                mv.setViewName("business/whitelist/whitelist_upload");
//                return mv;
//            }
            //批量保存到数据库
            try {
                whitelistService.saveAll(readList);
            } catch (Exception e) {
//                e.printStackTrace();
                logger.error(e.toString());
                errmsg = "系统异常,保存数据失败";
                mv.addObject("errmsg", errmsg);
                mv.setViewName("business/whitelist/whitelist_upload");
                return mv;
            }
            
        }
            
        mv.addObject("msg","success");
        mv.setViewName("save_result");
        return mv;
        
        
    }
    

y猪
246 声望25 粉丝