zoukankan      html  css  js  c++  java
  • SpringBoot导入excle文件数据

    本文主要描述,Springboot框架下上传excel,处理里面相关数据做逻辑分析,由于用到的是前后端分离技术,这里记录的主要是后端java部分,通过与前端接口进行对接实现功能

    1.在pom.xml文件中导入注解,主要利用POI

    <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi-ooxml</artifactId>
          <version>3.9</version>
    </dependency>
    <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.3.1</version>
    </dependency>
    <dependency>
          <groupId>commons-io</groupId>
          <artifactId>commons-io</artifactId>
          <version>2.4</version>
    </dependency>

    2. springboot java实现代码

    @RequestMapping(value="/uploadTest", method = RequestMethod.POST)
        public String uploadTest(@RequestParam MultipartFile file, HttpServletRequest request){
    
            try {
                if(file==null)
                    return BaseCode.retCode(1, "上传文件不能为空").toString();
                String fileName = file.getOriginalFilename();
                if (!fileName.matches("^.+\.(?i)(xls)$") && !fileName.matches("^.+\.(?i)(xlsx)$")) {
                    return BaseCode.retCode(1, "上传文件格式错误,请上传后缀为.xls或.xlsx的文件").toString();
                }
    
                boolean isExcel2003 = true;
                if (fileName.matches("^.+\.(?i)(xlsx)$")) {
                    isExcel2003 = false;
                }
                InputStream is = file.getInputStream();
                Workbook wb = null;
                if (isExcel2003) {
                    wb = new HSSFWorkbook(is);
                } else {
                    wb = new XSSFWorkbook(is);
                }
                Sheet sheet = wb.getSheetAt(0);
                if(sheet!=null){
                    //notNull = true;
                }
                for (int r = 1; r <= sheet.getLastRowNum(); r++) {
                    Row row = sheet.getRow(r);
                    if (row == null) {
                        continue;
                    }
                   
        System.out.println(row.getCell(0).getStringCellValue());
     } 
    }
    catch (IOException e) {
    }
    return BaseCode.retCode(ResultCode.success).toString();
    }

    3. PostMan调用方式:

     完毕!

  • 相关阅读:
    php aes128加密
    redis应用场景
    Yii2开发小技巧
    yii2开启事务
    yii2验证密码->手机号码短信发送>手机短信发送频繁问题
    yii2 rules验证规则,ajax验证手机号码是否唯一
    随笔20170415
    v1版本
    数的划分
    单词接龙
  • 原文地址:https://www.cnblogs.com/owenma/p/10099060.html
Copyright © 2011-2022 走看看