简单excel数据导入

实体类

public class BusiLandProjectExcel {
    /**
     * id
     */
    @ApiModelProperty(value = "id")
    @ExcelProperty(index = 0, value = "序号")
    private Integer id;

    /**
     * 农田项目名称
     */
    @ApiModelProperty(value = "农田项目名称")
    @ExcelProperty(index = 1, value = "农田项目名称")
    private String landProjectName;


    /**
     * 建设单位
     */
    @ApiModelProperty(value = "建设单位")
    @ExcelProperty(index = 4, value = "建设单位")
    private String buildUnit;

    /**
     * 建设地点
     */
    @ApiModelProperty(value = "建设地点")
    @ExcelProperty(index = 5, value = "建设地点")
    private String buildAddress;

    /**
     * 建设主要内容
     */
    @ApiModelProperty(value = "建设主要内容")
    @ExcelProperty(index = 6, value = "建设主要内容")
    private String buildMainContent;

    /**
     * 建设开始年份
     */
    @ApiModelProperty(value = "建设开始年份")
    @ExcelProperty(index = 7, value = "建设开始年份")
    private String buildStartYear;

    /**
     * 建设结束年份
     */
    @ApiModelProperty(value = "建设结束年份")
    @ExcelProperty(index = 8, value = "建设结束年份")
    private String buildEndYear;

    /**
     * 总建设面积_亩
     */
    @ApiModelProperty(value = "总建设面积_亩")
    @ExcelProperty(index = 9, value = "总建设面积_亩")
    private Double totalInvestment;

    /**
     * 是否完成验收
     */
    @ApiModelProperty(value = "是否完成验收")
    @ExcelProperty(index = 10, value = "是否完成验收")
    private String iCompleteAcceptance;

    /**
     * 实际完成建设面积_亩
     */
    @ApiModelProperty(value = "实际完成建设面积_亩")
    @ExcelProperty(index = 11, value = "实际完成建设面积_亩")
    private Double actualFinishInvestment;

    /**
     * 竣工验收时间
     */
    @ApiModelProperty(value = "竣工验收时间")
    @ExcelProperty(index = 12, value = "竣工验收时间")
    private Date completionTime;

    /**
     * 行政区划名称
     */
    @ApiModelProperty(value = "行政区划名称")
    @ExcelProperty(index = 13, value = "行政区划名称")
    private String administrativeDivisionName;

    /**
     * 行政区划代码
     */
    @ApiModelProperty(value = "行政区划代码")
    @ExcelProperty(index = 14, value = "行政区划代码")
    private String administrativeDivisionCode;


}

 监听类

package com.seerbigdata.village.business.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.seerbigdata.village.business.domain.excel.BusiLandProjectExcel;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: HuYongHui
 * @Date: 2024/5/22
 * Content:
 */
@Slf4j
public class BusiLandProjectListener extends AnalysisEventListener<BusiLandProjectExcel> {
    // 存储每个sheet的所有数据
    ArrayList<BusiLandProjectExcel> modelList = new ArrayList<>();

    @Override
    public void invoke(BusiLandProjectExcel busiLandProjectExcel, AnalysisContext analysisContext) {
        modelList.add(busiLandProjectExcel);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("所有数据解析完成!");
    }

    public List<BusiLandProjectExcel> getData() {
        return modelList;
    }

}

逻辑代码

 

@Override
    public Boolean upload(MultipartFile file) throws IOException{
        if (file == null) {
            throw new GlobalException("请选择上传文件");
        }
        BusiLandProjectListener listener = new BusiLandProjectListener();
        try {
            EasyExcel.read(file.getInputStream(), BusiLandProjectExcel.class, listener).sheet().doRead();
            // 获取数据列表
            List<BusiLandProjectExcel> data = listener.getData();
            if (CollectionUtil.isEmpty(data)) {
                throw new GlobalException("表格无数据,请重试!");
            }
            List<BusiLandProject> busiLandProjects = data.stream().map(busiLandProjectExcel -> {
                BusiLandProject busiLandProject = new BusiLandProject();
                BeanUtil.copyProperties(busiLandProjectExcel, busiLandProject);
                if ("是".equals(busiLandProjectExcel.getICompleteAcceptance())){
                    busiLandProject.setIsCompleteAcceptance(true);
                }else{
                    busiLandProject.setIsCompleteAcceptance(false);
                }
                busiLandProject.setFarmlandAttribute(HIGH_STANDARD_FARMLAND.getVal());
                return busiLandProject;
            }).collect(Collectors.toList());
            this.saveBatch(busiLandProjects);
        } catch (IOException e) {
            log.error("导入表格数据失败:{}".concat(e.getMessage()));
            throw new GlobalException("导入表格数据失败");
        }
        return Boolean.TRUE;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值