esayPOI导入Excel

对应excel字段名

package com.sf.controller;

import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Table;
import java.io.Serializable;

/**
 * @Author: yachun
 * @Date: 2019/5/23 13:29
 */
@Data
@Table(name = "tb_business_cooperation")
public class BusinessCooperationE implements Serializable {

	/**
	 * 企业名称
	 */
	@Column(name = "enterprise_name")
	@Excel(name = "供应商")
	private String enterpriseName;

	/**
	 * 业务联系人
	 */
	@Column(name = "business_contact")
	@Excel(name = "姓名")
	private String businessContact;

	/**
	 * 联系方式
	 */
	@Column(name = "contact_information")
	@Excel(name = "电话号")
	private String contactInformation;

	/**
	 * 办公城市
	 */
	@Column(name = "office_city")
	@Excel(name = "接单城市")
	private String officeCity;




}

controller层

@PostMapping(value = "importExcelTest", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public Response importExcel(@RequestPart(value = "file") MultipartFile file,
								HttpServletRequest request, HttpServletResponse response) throws IOException {
		log.info("接收到上传的信息");
		ImportParams importParams = new ImportParams();
		// 需要验证
		importParams.setNeedVerfiy(false);
		try {
			ExcelImportResult<BusinessCooperationE> result = ExcelImportUtil.importExcelMore(
					file.getInputStream(), BusinessCooperationE.class, importParams);
			List<BusinessCooperationE> successList = result.getList();
			List<BusinessCooperationE> failList = result.getFailList();
			log.info("是否存在验证未通过的数据:" + result.isVerfiyFail());
			log.info("验证通过的数量:" + successList.size());
			log.info("验证未通过的数量:" + failList.size());
			//调用方法把表格插入到数据库
			int i = 0;
			int k = 0;
			for (BusinessCooperationE record : successList) {
				BusinessCooperationDO businessCooperationDO = new BusinessCooperationDO();
				BeanUtils.copyProperties(record, businessCooperationDO);
				businessCooperationDO.setAuditStatus(1);
				IdWorker id = new IdWorker();
				businessCooperationDO.setId(id.nextId());

				//TODO 供应商代码规则-- sup + 城市代码+ 001
				String officeCityTemp = record.getOfficeCity();
				String codeByCityTemp = CityAndCodeUtil.getCodeByCity(officeCityTemp);

				Integer providerCodeCount = businessCooperationService.countProviderCode("sup".concat(codeByCityTemp));
				log.info("获取到的总编码个数是---->" + providerCodeCount);

				String  resultProviderCode= null;
				if (providerCodeCount > 0) {
					//中间数字
					String sandwichDigit = "00";

					if (providerCodeCount >= BusinessConstant.NINE) {
						sandwichDigit = "0";
					}
					if (providerCodeCount >= BusinessConstant.NINETY_NINE) {
						sandwichDigit = "";
					}

					String newProviderCode = BusinessConstant.SUP + codeByCityTemp + sandwichDigit + (providerCodeCount + 1);
					resultProviderCode = newProviderCode;
					businessCooperationDO.setProviderCode(newProviderCode);
					businessCooperationDO.setSupplierAccount(newProviderCode);
				} else {
					String officeCity = record.getOfficeCity();
					String codeByCity = CityAndCodeUtil.getCodeByCity(officeCity);
					resultProviderCode = BusinessConstant.SUP.concat(codeByCity.concat(BusinessConstant.INIT_PROVIDER_CODE));
					businessCooperationDO.setProviderCode(BusinessConstant.SUP.concat(codeByCity.concat(BusinessConstant.INIT_PROVIDER_CODE)));
					businessCooperationDO.setSupplierAccount(BusinessConstant.SUP.concat(codeByCity.concat(BusinessConstant.INIT_PROVIDER_CODE)));
				}

				int j =  businessCooperationService.insertSelective(businessCooperationDO);
				if (j == 0) {
					k++;
				}
				i += j;
			}
			log.info("上传文件成功");
			return ResponseHelper.buildOk("验证通过的数量:" + successList.size() + "验证未通过的数量:" + failList.size() + "重复或数据错误的数量" + i + "上传操作成功,上传数据" + i + "条");
		} catch (Exception e) {
			log.info("导入异常,请检查手机号是否已存在", e);
			return ResponseHelper.buildFail("导入异常,请检查手机号是否已存在");
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值