easyexcel 导入导出

 EasyExcel技术,是Alibaba集团开源的,该技术是针对Apache POI技术的封装和优化,主要解决了POI技术的耗内存问题,并且提供了较好的API使用。不需要大量的代码就可以实现excel的操作功能。

pom文件新增:

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.6</version>
        </dependency>

实体类: 注意这6个注解,都是需要的,少的话测试的时候会报错的

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
@HeadRowHeight(20)
@ColumnWidth(20)
@ContentRowHeight(15)
public class StudentDo {

    @ExcelProperty(index = 0, value = "学号")
    private String sno;

    @ExcelProperty(index = 1, value = "姓名")
    private String name;

    @ExcelProperty(index = 2, value = "年龄")
    private Integer age;

    @ExcelProperty(index = 3, value = "性别")
    private String gender;

    @ExcelProperty(index = 4, value = "籍贯")
    private String nativePlace;

    @ExcelProperty(index = 5, value = "入学时间")
    private Date enrollmentTime;

}

监听类:监听实体类

public class StudentListener extends AnalysisEventListener<StudentDo> {

    @Getter
    private List<StudentDo> studentList = new ArrayList<StudentDo>();

    public StudentListener() {
        super();
        studentList.clear();
    }

    /**
     * 每一条数据解析都会调用
     */
    @Override
    public void invoke(StudentDo student, AnalysisContext context) {
        studentList.add(student);
    }

    /**
     * 所有数据解析完成都会调用
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        studentList.forEach(System.out::println);
    }

}

interface文件:

 @GetMapping("/export")
    public void exportStudentInfos(HttpServletResponse response, HttpServletRequest request)
            throws IOException, ParseException;
@RequestMapping(value = "import")
    public void importStudentInfos(MultipartFile file) throws IOException;

interfaceImpl文件:

 @Override
    public void exportStudentInfos(HttpServletResponse response, HttpServletRequest request) throws IOException, ParseException {
        // 设置响应类型
        response.setContentType("application/vnd.ms-excel");
        // 设置字符编码
        response.setCharacterEncoding("utf-8");
        // 设置响应头信息
        response.setHeader("Content-disposition",
                "attachment;filename*=utf-8''" + URLEncoder.encode("学生花名册", "UTF-8") + ".xlsx");

        List<StudentDo> studentList = new ArrayList<StudentDo>() {
            {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                add(new StudentDo("1001", "张三", 23, "男", "陕西西安", dateFormat.parse("2020-09-01")));
                add(new StudentDo("1002", "李四", 22, "女", "陕西渭南", dateFormat.parse("2020-09-01")));
            }
        };

        // 写入文件
        EasyExcel.write(response.getOutputStream(), StudentDo.class).sheet("学生信息").doWrite(studentList);

    }

    @Override
    public void importStudentInfos(MultipartFile file) throws IOException {
        StudentListener studentListener = new StudentListener();
        EasyExcel.read(file.getInputStream(), StudentDo.class, studentListener).sheet().doRead();
        studentListener.getStudentList();
    }

启动项目,去postman测试,即可导出文件

测试导入,把刚才导出的文件导入进来

打上断点就能看到导入的数据了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

65页ppt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值