springboot-文件上传xls及POI操作Excel

1、pom导入依赖文件

        <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、采用Multipart接收,设置接收大小

spring.http.multipart.maxFileSize=10Mb
spring.http.multipart.maxRequestSize=10Mb

3、controller层,用@RequestParam接收

@RequestMapping(value = "/resultImport",method = RequestMethod.POST)
    @ResponseBody
    public ResultBean importResult(@RequestParam("file") MultipartFile file) throws Exception{
        ResultBean resultBean =new ResultBean();
        String fileName = file.getOriginalFilename();
        if(askService.batchImport(fileName,file) ==0){
            resultBean.setMsg("上传文件格式不正确");
            resultBean.setCode(1);
        }else{
            resultBean.setMsg("导入成功");
        }
        return resultBean;
    }

4、service层

//客户批量导入
    public Integer batchImport(String fileName, MultipartFile file) throws Exception{
        boolean notNull = false;
        Integer status = 1;
        List<ResultInfo> resultList = new ArrayList<>();

        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            String error = "上传文件格式不正确";
            status = 0;
            return status;
        }
        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;
        }
        System.out.println(sheet.getLastRowNum());
        for (int r = 1; r < sheet.getLastRowNum()-1; r++) {
            Row row = sheet.getRow(r);
            if (row == null){
                continue;
            }
            ResultInfo resultInfo = new ResultInfo();
            AskUserInfo askUserInfo = new AskUserInfo();


            row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);//设置读取转String类型
            row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
            row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
            row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
            row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
            row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);


            String testId = row.getCell(1).getStringCellValue();
            String name = row.getCell(3).getStringCellValue();
            String record = row.getCell(4).getStringCellValue();
            String sex = row.getCell(5).getStringCellValue();
            String age = row.getCell(6).getStringCellValue();

String idCard = row.getCell(7).getStringCellValue(); if(testId ==null || name ==null || sex==null || age==null){ continue; } askUserInfo.setName(name); askUserInfo.setRecord(record); if(sex.equals("1")){ askUserInfo.setSex(1); }else{ askUserInfo.setSex(0); } askUserInfo.setIdcard(idCard); askUserInfo.setAge(Integer.parseInt(age)); resultInfo.setTestId(testId); resultInfo.setCreateTime(new Date()); System.out.println(r + name); AskUserInfo askUserInfo1 =askUserInfoRepository.save(askUserInfo); resultInfo.setAskUserInfo(askUserInfo1); resultInfoRepository.save(resultInfo); } return status; }

 

5、前端js提交

impData:function(){
            var vm = this;
            var inputDOM = this.$refs.inputer;
            var formdata = new FormData();
            formdata.append('file',inputDOM.files[0]);
            vm.$http.post('/admin/resultImport',formdata).then(function (res) {
                var data = res.data;
                if(data.code==0) {
                   alert("导入成功");
                    window.history.back(-1);
                }
                console.log(data);
            })
        }

6、前端html

<input type="file"  placeholder="请选择文件" name="file" ref="inputer">

7、测试

posted @ 2018-08-15 11:40  故事里的牧羊人  阅读(6375)  评论(0编辑  收藏  举报