java springboot api接口使用easyexcle导出xlsx

提示

使用这种方式,会使你的项目打jar包后,体积增加20MB左右(仅为个人测试)
如果你对表格样式、格式没有要求,可以曲线救国,代码看我其他文章

添加pom依赖

        <!--导出excle依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.0.5</version>
        </dependency>

实体类

package cn.myauthx.api.main.entity;

import cn.myauthx.api.base.po.baseEntity;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.alibaba.excel.annotation.write.style.HeadStyle;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.Date;

/**
 * <p>
 * 
 * </p>
 *
 * @author DaenMax
 * @since 2022-01-06
 */
@Data
@Accessors(chain = true)
@TableName("ma_card")
public class Card extends Model {

    private static final long serialVersionUID = 1L;
    @ExcelProperty(value = "ID",index = 0)
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
    @ExcelProperty(value = "卡密",index = 1)
    private String ckey;

    /**
     * 点数
     */
    @ExcelProperty(value = "点数",index = 2)
    private Integer point;

    /**
     * 秒数
     */
    @ExcelProperty(value = "秒数",index = 3)
    private Integer seconds;

    /**
     * 生成时间
     */
    @ExcelIgnore //表明我在写的时候不用这列
    private Integer addTime;

    /**
     * 使用时间
     */
    @ExcelIgnore //表明我在写的时候不用这列
    private Integer letTime;

    /**
     * 生成时间
     */

    @ExcelProperty(value = "生成时间",index = 4)
    @TableField(exist = false)
    private Date addTimeName;

    /**
     * 使用时间
     */
    @ExcelProperty(value = "使用时间",index = 5)
    @TableField(exist = false)
    private Date letTimeName;

    /**
     * 使用人账号
     */
    @ExcelProperty(value = "使用人账号",index = 6)
    private String letUser;

    /**
     * 卡密状态,0=未使用,1=已使用,2=被禁用
     */
    @ExcelIgnore //表明我在写的时候不用这列
    private Integer status;

    /**
     * 卡密状态,0=未使用,1=已使用,2=被禁用
     */
    @ExcelProperty(value = "卡密状态",index = 7)
    @TableField(exist = false)
    private String statusName;

    /**
     * 所属软件id
     */
    @ExcelIgnore //表明我在写的时候不用这列
    private Integer fromSoftId;

    /**
     * 所属软件名称
     */
    @ExcelProperty(value = "所属软件",index = 8)
    @TableField(exist = false)
    private String fromSoftName;

}

工具类

/**
     * 导出XLS
     *
     * @param request
     * @param response
     * @param fileName 导出的文件名,不需要加.xlsx
     * @param sheetName 工作表名
     * @param list<T>
     * @param entityClass
     */
    public static <T> void exportXls(HttpServletRequest request, HttpServletResponse response, String fileName, String sheetName, List<T> list,Class entityClass) {
        //因为我项目的特殊性,所以必须先getSession,否则会报错
        request.getSession();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        try {
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ExcelWriterBuilder write = EasyExcel.write(outputStream, entityClass);
        ExcelWriterSheetBuilder sheet = write.sheet(sheetName);
        sheet.doWrite(list);
        return;
    }
posted @ 2022-03-07 21:31  DaenMax  阅读(172)  评论(0编辑  收藏  举报