vue2传送excel文件到后端, springboot解析excel

 <el-button type="primary"  @click="excelDialogVisible = true">excel 批量导入</el-button>
 <!-- 导入弹窗-->
 <el-dialog title="excel导入" :visible.sync="excelDialogVisible" :close-on-click-modal="false" :modal-append-to-body="false" :before-close="handleExcelDialogClose"  width="50%" center>
          <el-upload
            class="upload-demo"
            ref="upload"
            :action="upLoadUrl"
            :data="getNowId"
            :on-progress="onProgress"
            :on-success="onSuccess"
            :beforeUpload="beforeUpload"
            :limit="1"
            accept=".xlsx,.xls"
            :auto-upload="true">
            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
<!--            <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>-->
            <div slot="tip" class="el-upload__tip">只能上传xlsx/xls文件,且不超过500kb</div>
          </el-upload>

        </el-dialog>
<script>
export default {
  data(){
    return {

      //excel导入
      excelDialogVisible: false,
      excelFile:{},
      uploading:false,
      upLoadUrl: window.atob(window.SITE_CONFIG.baseUrl) + '/customer/upExcel.upload',
      // 上传文件需要携带的其他参数
      // fileUploadParam: {
      //   userId:this.$store.state.user.id,
      // },
    }
  }

  //计算属性 页面加载时自动赋值
  computed:{
    // 获取当前登陆用户名 id
    getNowId(){ return {id:this.id,} },
  },

  methods:{
    // upExcel,
    handleExcelDialogClose(done) {
      this.$confirm('确认关闭?')
        .then(_ => {
          this.$refs.upload.clearFiles() //清除上传文件对象
          done();
        })
        .catch(_ => {});
    },
    handleExceed() {
      this.$message.warning("仅允许上传一个文件!");
    },
    onProgress(){
      this.uploading = true
    },
    onSuccess(res, file, fileList){
      //显示添加成功的条数 并刷新
      this.$refs.upload.clearFiles() //清除上传文件对象
      this.excelDialogVisible = false
      this.$message.success("成功添加 "+res.successedNum+" 条客户")
      this.handlerQuery();
    },
  }

}


</script>
    @PostMapping("/upExcel.upload")
    @ApiOperation(value = "导入", notes = "excel导入客户信息", produces = MediaType.APPLICATION_JSON_VALUE)
    public HashMap<String, String> upExcel(@RequestParam("id") Integer id, @RequestParam("file") MultipartFile excelFile) {
        return customerInfoService.handelExcel(id, excelFile);
    }
    //业务层实现
    @Override
    public String getCustomerInfo(CustomerInfoDTO customerInfoDTO) {
        Page page = packSearchPage(customerInfoDTO);
        IPage<CustomerInfoVO> ipage = this.baseMapper.getCustomerInfo(page, customerInfoDTO);
        List<CustomerInfoVO> list = ipage.getRecords();
        String str = "{\"total\":" + ipage.getTotal() + ",\"size\":" + ipage.getSize() + ",\"current\":" + ipage.getCurrent()
                + ",\"data\":" + JSONArray.fromObject(list).toString() + "}";
        return str;
    }

    @Override
    @Transactional
    public String addCustomerInfo(CustomerAllFieldDTO customerAllFieldDTO) {
        if (customerAllFieldDTO.getCustomerPhone().equals("") || customerAllFieldDTO.getCustomerPhone() == null) {
            return "请输入客户号码";
        }
        if (customerAllFieldDTO.getCustomerName().equals("") || customerAllFieldDTO.getCustomerName() == null) {
            return "请输入客户姓名";
        }
        int flag = this.baseMapper.getIdByNum(customerAllFieldDTO.getCustomerPhone());
        if (flag > 0) {
            return "已存在此号码,请勿重复添加";
        } else {
            // 插入客户与标签关联关系表
            int insertFlag = this.baseMapper.addCustomerInfo(customerAllFieldDTO);
            return insertFlag > 0 ? "新增用户成功" : "新增用户失败";
        }
    }


    public class ExcelListener extends AnalysisEventListener<CustomerImportEntity> {
        private List<CustomerImportEntity> dataList = new ArrayList<>();
        @Override
        public void invoke(CustomerImportEntity CustomerImportEntity, AnalysisContext analysisContext) {
            dataList.add(CustomerImportEntity);
        }
        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
            // 解析完成后的操作,可以对 dataList 进行后续处理
            log.info("---analysisContext:{}",analysisContext);
        }
        public List<CustomerImportEntity> getDataList() {
            return dataList;
        }
    }
    @Override
    public List<CustomerImportEntity> parseExcel(InputStream inputStream) {
        ExcelListener listener = new ExcelListener();
        EasyExcel.read(inputStream, CustomerImportEntity.class, listener).sheet().doRead();
        return listener.getDataList();
    }

@Override
    public HashMap<String, String> handelExcel(Integer id, MultipartFile excelFile) {
        HashMap<String, String> resmap = new HashMap<>();
        resmap.put("successedNum", "");
        try {
            InputStream inputStream = excelFile.getInputStream();
            List<CustomerImportEntity> dataList = parseExcel(inputStream);
            int num = 0;
            for (CustomerImportEntity importDTO : dataList) {
                CustomerAllFieldDTO allFieldDTO = new CustomerAllFieldDTO();
                allFieldDTO.setCustomerPhone(importDTO.getPhone());
                allFieldDTO.setCustomerName(importDTO.getCustomerName());
                allFieldDTO.setCreateStaff(id);
                allFieldDTO.setCustomerOptStaff(id);
//                BeanUtils.copyProperties(DTO,allFieldDTO);
                String oneRes = addCustomerInfo(allFieldDTO);
                resmap.put(importDTO.getOrderNumber(), oneRes);
                if ("新增用户成功".equals(oneRes)) {
                    num++;
                }
            }
            resmap.replace("successedNum", "", Integer.toString(num));

        } catch (IOException e) { e.printStackTrace(); }
        return resmap;
    }
    <insert id="addCustomerInfo">
        insert into gc_ccsp_customer(customer_phone, customer_name, create_staff, create_time, customer_opt_staff, customer_opt_time, is_del)
        values(#{customerPhone}, #{customerName}, #{createStaff}, now(), #{createStaff}, now(), 0)
    </insert>

    <select id="getCustomerInfo" resultType="com....custom.vo.CustomerInfoVO">
        select a.*,group_concat(b.customer_tag_id) as customerTagIds,group_concat(c.customer_tag_name) as customerTagNames,
        d.real_name from gc_ccsp_customer a
        left join gc_ccsp_customer_tag_rel b on a.CUSTOMER_ID = b.CUSTOMER_ID
        left join gc_ccsp_customer_tag c on b.customer_tag_id = c.customer_tag_id
        left join gc_user d on a.customer_opt_staff = d.id
        where a.is_del = 0
        <if test="customerInfoDTO.searchTimeBegin !=null and customerInfoDTO.searchTimeBegin !=''">
            and a.customer_opt_time &gt; #{customerInfoDTO.searchTimeBegin}
        </if>
        <if test="customerInfoDTO.searchTimeEnd !=null and customerInfoDTO.searchTimeEnd !=''">
            and a.customer_opt_time &lt; #{customerInfoDTO.searchTimeEnd}
        </if>
        <if test="customerInfoDTO.customerName !=null and customerInfoDTO.customerName !=''">
            and a.customer_name like CONCAT ('%',#{customerInfoDTO.customerName},'%')
        </if>
        <if test="customerInfoDTO.customerPhone !=null and customerInfoDTO.customerPhone !=''">
            and a.customer_phone like CONCAT ('%',#{customerInfoDTO.customerPhone},'%')
        </if>
        group by a.CUSTOMER_ID
    </select>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值