poi导入Execl数据

 @GetMapping("/excel/upload")
    public void importExcel(@RequestParam("excelFile") MultipartFile multipartFile) throws IOException {


        if (multipartFile==null|| multipartFile.getSize()==0){
            log.error("文件上传错误,重新上传");
        }
        String filename = multipartFile.getOriginalFilename();
        if (!(filename.endsWith(".xls")|| filename.endsWith(".xlsx"))){
            log.error("文件上传格式错误,请重新上传");
        }
        List<RuleVo> ruleVoList = readXLS(multipartFile);
        System.out.println("完毕");


    }

    public List<RuleVo> readXLS(MultipartFile file) throws IOException {
        List<RuleVo> ruleVoList =new ArrayList<>();

        InputStream inputStream = file.getInputStream();
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);

        //读取第一张sheet
        HSSFSheet sheet = workbook.getSheetAt(0);
        String errorMsg="";
        //遍历每一行Excel获取内容
        for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
            HSSFRow row = sheet.getRow(rowNum);
            if (row!=null){
                RuleVo ruleVo = new RuleVo();
                //使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
                //所以先使用setCellType()方法先将该单元格的类型设置为STRING
                //然后poi会根据字符串读取它
                row.getCell(0).setCellType(CellType.STRING);
                HSSFCell cell = row.getCell(1);
                //todo  有指针异常风险 row.getCell记得判断。
                row.getCell(1).setCellType(CellType.STRING);
                row.getCell(2).setCellType(CellType.STRING);
                row.getCell(3).setCellType(CellType.STRING);
                row.getCell(5).setCellType(CellType.STRING);
                row.getCell(6).setCellType(CellType.STRING);
                ruleVo.setName(row.getCell(0).getStringCellValue());
                ruleVo.setGroupId(Long.parseLong(row.getCell(1).getStringCellValue()));
                ruleVo.setChannel(row.getCell(2).getStringCellValue());
                ruleVo.setVersion(Integer.parseInt(row.getCell(3).getStringCellValue()));
                String ruleContentList = row.getCell(5).getStringCellValue();
                RuleContent[] array = new Gson().fromJson(ruleContentList,RuleContent[].class);
                List<RuleContent> list = Arrays.asList(array);
                ruleVo.setContentList(list);
                ruleVo.setOperator(row.getCell(6).getStringCellValue());
                ruleVo.setCreateTime(row.getCell(7).getDateCellValue());
                ruleVo.setUpdateTime(row.getCell(8).getDateCellValue());
                ruleVoList.add(ruleVo);
            }
        }
        return ruleVoList;
    }

全部评论

相关推荐

06-26 15:33
青岛工学院 Java
积极的秋田犬要冲国企:他现在邀请我明天面试
点赞 评论 收藏
分享
Twilight_m...:表格简历有点难绷。说说个人看法: 1.个人基本情况里好多无意义信息,什么婚姻状况、健康状况、兴趣爱好、户口所在地、身份证号码、邮政编码,不知道的以为你填什么申请表呢。 2.校内实践个人认为对找工作几乎没帮助,建议换成和测开有关的项目,实在没得写留着也行。 3.工作经历完全看不出来是干什么的,起码看着和计算机没啥关系,建议加强描述,写点你在工作期间的实际产出、解决了什么问题。 4.个人简述大而空,看着像AI生成,感觉问题最大。“Python,C,C++成为我打造高效稳定服务的得力工具”、“我渴望凭借自身技术知识与创新能力,推动人工智能技术的应用发展,助力社会实现智能化转型”有种小学作文的美感。而且你确定你个人简述里写的你都会嘛?你AI这块写的什么“深入研究”,发几篇顶会的硕博生都不一定敢这么写。而且你AI这块的能力和软测也完全无关啊。个人简述建议写你对哪些技术栈、哪些语言、哪些生产工具的掌握,写的有条理些,而且最好是和测开强相关的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务