Springboot下使用easyExcel玩转Excel表的导出

1、Excel表格的导出:

1、搭建一个springboot项目:

https://blog.csdn.net/LemonSnm/article/details/87894940

 2、maven中加入EasyExcel所需包

        
        
            com.alibaba
            easyexcel
            2.1.6
        

2、模拟学生实体类Student

package com.lemon.entity;


import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;

/**
 * 学生类
 * @author lemon
 * @since 2020-05-26
 */
@ExcelIgnoreUnannotated()
public class Student {
    
    private Integer myId;

    @ExcelProperty(value = "学号",index = 0) //0 对应导出Excel表格的第一列
    private Integer id;

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

    @ExcelProperty(value = "性别",index = 2)
    private String sex;

    @ExcelProperty(value = "班级",index = 3)
    private String grade;

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", grade='" + grade + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }


}

3、导出功能controller:

package com.lemon.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.StringUtils;
import com.lemon.easyutils.EasyExcelListener;
import com.lemon.entity.Student;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.FileInputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 测试controller
 * @author lemon
 * @since 2020/5/26 0026
 */
@Controller
@RequestMapping("/test")
public class TestController {

/**
 * 主页面
 * @author lemon
 * @since 2020/5/26 0026
 */
    @RequestMapping("/index")
    public String index(){
        return "/views/index";
    }

    /**
     * 导出
     * @author lemon
     * @since 2020/5/26 0026
     */
    @RequestMapping("/export")
    public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {


        //模拟需要导出的数据
        List list = new ArrayList();
        Student student = new Student();
        student.setId(1);
        student.setName("1321");
        student.setSex("男");
        student.setGrade("一年级");
        for (int i = 0; i < 5; i++) {
            list.add(student);
        }

        //设置并导出
        // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        //设置文件名
        SimpleDateFormat fDate = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String fileName = "导出测试表" + fDate.format(new Date()) + ".xlsx";
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        EasyExcel.write(response.getOutputStream(), Student.class).sheet("sheet1").doWrite(list);
    }

}

4、导出功能的页面:



    测试







5、测试:

输入http://localhost:8081/test/index

Springboot下使用easyExcel玩转Excel表的导出_第1张图片

点击导出 选择导出位置:

Springboot下使用easyExcel玩转Excel表的导出_第2张图片

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(easyExcel)