阿里开源EasyExcel(导出Excel)

阿里开源EasyExcel(导出Excel)

阿里开源EasyExcel(导出Excel)_第1张图片

controller类

@RequestMapping("/exports")
    @ResponseBody
    public void exports(HttpServletRequest request,HttpServletResponse response){

        List< Students > exports = studentService.exports();
        try {
            ServletOutputStream out = response.getOutputStream();
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX,true);
            String fileName = new String("Student信息表".getBytes("utf-8"),"iso8859-1");
            Sheet sheet = new Sheet(1,0,Students.class);
            sheet.setSheetName("sheet1");
            response.setHeader("Content-disposition", "attachment;filename="+fileName+".xlsx");
            response.setContentType("multipart/form-data");
            response.setCharacterEncoding("utf-8");
            writer.write(exports,sheet);
            writer.finish();
            out.flush();
        }catch (Exception e) {

            e.printStackTrace();

        }

    }

Students类

package com.tuanzi.entity;
import	java.io.Serializable;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;

/**
 * @auther 团子
 * @date 2019-09-04 22:01
 */

public class Students extends BaseRowModel implements Serializable {

    private static final long serialVersionUID = 15353L;

    @ExcelProperty(value = "主键id",index =0)
    private String id;

    @ExcelProperty(value = "学号",index =1)
    private String number;

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

    @ExcelProperty(value = "身份类型",index =3)
    private String type;

    @ExcelProperty(value = "登录密码",index =4)
    private String password;

    public String getId() {
        return id;
    }

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

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

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

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

注:@ExcelProperty(value = “xxxxx”,index =3)其中value 为列名,index 为列的序号

service类

 public List< Students> exports() {
        return studentMapper.exports();
    }

mapper

 List< Students> exports();

mapper.xml文件


    
    id, number, name, type, password
  

运行看看效果:
阿里开源EasyExcel(导出Excel)_第2张图片
阿里开源EasyExcel(导出Excel)_第3张图片

你可能感兴趣的:(SpringBoot)