Spring Boot集成easyExcel实现excel导入导出功能

一、导出

1、引入依赖


    com.alibaba
    easyexcel
    最新版本号

2、定义导出数据传输对象

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.*;
import com.alibaba.excel.enums.BooleanEnum;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import lombok.Data;

import java.time.LocalDateTime;

@Data
@HeadFontStyle(bold = BooleanEnum.TRUE, color = 53)
@HeadRowHeight(23)
@HeadStyle(fillForegroundColor = 40, horizontalAlignment = HorizontalAlignmentEnum.CENTER)
@ContentFontStyle(color = 54)
@ContentRowHeight(15)
@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER)
public class ExcelParam {
    @ExcelProperty(order = 1, value = "姓名")
    @ColumnWidth(20)
    private String name;
    @ExcelProperty(order = 2, value = "性别")
    @ColumnWidth(20)
    private String sex;
    @ExcelProperty(order = 3, value = "年龄")
    @ColumnWidth(20)
    private Integer age;
    @ExcelProperty(order = 4, value = "住址")
    @ColumnWidth(30)
    private String address;
    @ExcelProperty(order = 5, value = "参加工作时间")
    @ColumnWidth(30)
    private LocalDateTime jobTime;
}

3、实现导出功能

@SneakyThrows
@GetMapping("/export")
public void exportExcel(HttpServletResponse response) {
    List excelParams = Lists.newArrayList();
    for (int i = 0; i < 10; i++) {
        ExcelParam excelParam = new ExcelParam();
        excelParam.setName("测试" + i);
        excelParam.setSex("男");
        excelParam.setAge(i);
        excelParam.setAddress("福建省厦门市");
        excelParam.setJobTime(LocalDateTime.now());
        excelParams.add(excelParam);
    }
    String excelName = "个人信息登记表";
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("utf-8");
    excelName = URLEncoder.encode(excelName, "UTF-8");
    response.setHeader("Content-disposition", "attachment;filename=" + 
            excelName + ExcelTypeEnum.XLSX.getValue());
    EasyExcelFactory.write(response.getOutputStream(), ExcelParam.class).
                    sheet("个人信息登记表").doWrite(excelParams);
}

二、导入

1、定义监听器

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.springboottest.ExcelParam;
import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public class UploadExcelListener extends AnalysisEventListener<ExcelParam> {
    /**
     * 每隔500条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
     */
    private static final int BATCH_COUNT = 500;
    private final List<ExcelParam> excelParamList = new ArrayList<>();

    /**
     * 每一条数据解析都会来调用
     */
    @Override
    public void invoke(ExcelParam excelParam, AnalysisContext analysisContext) {
        //解析数据,暂存excelParamList
        excelParamList.add(excelParam);
    }

    /**
     * 所有数据解析完成了 都会来调用
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //数据持久化到数据库
        log.info(excelParamList.toString());
    }
}

2、实现导入功能

/**
 * 导入excel
 */
@SneakyThrows
@PostMapping("/upload")
public String importExcel(@RequestPart("file") MultipartFile multipartFile) {
    EasyExcelFactory.read(multipartFile.getInputStream(), ExcelParam.class, new UploadExcelListener()).
                     sheet().doRead();
    return "ok";
}

你可能感兴趣的:(excel,spring,boot,java)