excel 가져오기, excel 데이터 읽기

7668 단어

첫 번째 sheet 가져오는 방법


HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(this.getExcel())); HSSFSheet sheet = wb.getSheetAt(0);
//this.getExcel() excel 
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(this.getExcel()));
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(1);
Json jsonMsg = new Json();
if(row == null){
    jsonMsg.setSuccess(false);
    jsonMsg.setMsg(" , !");
}else{
    String msg = getExcelContent(sheet);
        jsonMsg.setSuccess(true);
        jsonMsg.setMsg(msg);
}
this.writeJson(jsonMsg);
return NONE;

getExcelContent excel 데이터 처리 방법

 private String getExcelContent(HSSFSheet sheet) {
       
    
        HSSFRow row;
     
        try {
            //  
            int rowNum = sheet.getLastRowNum();
            //  , 
            for (int i = 3; i <=rowNum; i++) {
                Grade grade = new Grade();
                row = sheet.getRow(i);
                if(row != null){

                    if(row.getCell(1) !=null){ //  
                        Object [] objects = findIdByIdCard(row.getCell(1).toString() , this.clazzOpenId);
                        if(objects.length == 0){ // 
                            idcardNotFoundNum++;
                            continue;
                        }
                        Integer signDetailId = Integer.parseInt(objects[0].toString());
                        Integer personId = Integer.parseInt(objects[1].toString());
                        Integer clazzOpenId = this.clazzOpenId ;
                        grade.setPersonId(personId);
                        grade.setSignDetailId(signDetailId);
                        grade.setClazzOpenId(clazzOpenId);
                    }else{
                        idcardBlank++;
                        continue;
                    }


                    if(row.getCell(5) !=null && row.getCell(5).toString() != "" ){ //  
                        String score = row.getCell(5).toString().trim();
                        if(" ".equals(score)){
                            score = "-1";
                        }else if(" ".equals(score)){
                            score = "-2";
                        }else if(" ".equals(score)){
                            score = "-3";
                        }else if(" ".equals(score)){
                            score = "-4";
                        }
                        grade.setScore(Double.parseDouble(score));
                    }
                    if(StringUtils.isNotBlank(row.getCell(6).toString().trim())){ //  
                        String result = row.getCell(6).toString().trim();
                         if (" ".equals(result)){
                            result = "1";
                             grade.setResult(Integer.parseInt(result));
                        }else if(" ".equals(result)){
                            result = "0";
                             grade.setResult(Integer.parseInt(result));
                        }
                        grade.setStatus(RELEASE_NO);
                    }else{
                        grade.setStatus(RELEASE_NO);
                    }
                    if( grade.getPersonId() != null ){
                        Map map = scoreManageDao.countByPersonId(grade.getPersonId() , this.clazzOpenId);
                        if(map.size() == 0 ){
                            grade.setCreateOperator(getLoginUser().getId().toString());
                            grade.setCreateTime(new Date());
                            scoreManageDao.save(grade);
                            successNum++;
                        }else{
                            if(map.get("id") != null ){
                                grade.setId(Integer.parseInt(map.get("id")));
                            }
                            grade.setUpdateOperator(getLoginUser().getId().toString());
                            grade.setUpdateTime(new Date());
                            scoreManageDao.update(grade);
                            updateNum++;
                        }
                    }
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
   
        return sb.toString();
    }

두 번째sheet 획득 방법


File temp = null; temp = new File(ExcelUploadFileUtil2.upload(file)); ExcelReader readerQuestions = null; readerQuestions = ExcelUtil.getReader(temp, 0);//첫 번째 테이블 가져오기
 @RequestMapping(value = "/questions/",method = {RequestMethod.POST}, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Result uploadQuestions(@CurrentAdmin UserAdminEntity admin, @RequestParam(value = "file", required = true) MultipartFile file) throws Exception {
        //  
        File temp = null;
        ExcelReader readerQuestions = null;
        ExcelReader readerOptions = null;
        List rePath = null;
        if (file != null && !file.isEmpty()){
            try {
                temp = new File(ExcelUploadFileUtil2.upload(file));
                readerQuestions = ExcelUtil.getReader(temp, 0);   // 
                readerOptions = ExcelUtil.getReader(temp, 1); // 
                rePath = ExcelFileReadUtil.makePath(temp.getPath());  
            } catch (Exception e){
                e.printStackTrace();
                throw new FileUploadException(" ");
            }

            //  
            List re = questionService.insertQuestionList(readerQuestions, admin.getUserName(), LocalUtil.create("q", new ArrayList()).get());

            //  
            questionService.insertQuestionOptionsList(readerOptions, admin.getUserName(), LocalUtil.get("q", ArrayList.class), LocalUtil.create("o", new ArrayList()).get());

            //  
            questionService.insertQuestionOptionPhotoList(QuestionsFileVo.make(rePath), admin.getUserName(), LocalUtil.get("q", ArrayList.class), LocalUtil.get("o", ArrayList.class));

            //  
            LocalUtil.remove("q");
            LocalUtil.remove("o");
            return new Result(200, "", re);
        }
        throw new FileUploadException(" ");
    }

테이블 데이터 처리
  /**
     *  
     */
    public List insertQuestionList(ExcelReader reader, String name, List rongQi) throws ParamException, ResultException, IOException, EmptyException {
        long pid = 0;
        //  excel 
        List> readAll = reader.read();
        for (int i = 0; i < readAll.size(); i++){
            if (i < 1){
                continue;
            }
            List reaa = readAll.get(i);
            try {
                if (reaa.get(7) == null || reaa.get(7).toString().equals("")){
                    continue;
                }
                Long rea = handler(reaa, pid, takeKnowledge(reaa.get(5)), name, rongQi);
                pid = rea;
            } catch (Exception e){
                e.printStackTrace();
                List rea = new ArrayList<>();
                rea.add(handlerResult(readAll.get(i), e.getMessage()));
                return rea;
            }
        }
        return new ArrayList<>();
    }

좋은 웹페이지 즐겨찾기