欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot+easypoi导出excel

程序员文章站 2024-03-21 19:54:40
...

相关依赖

<dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.2.0</version>
        </dependency>

实体类

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;
@Data
@TableName("sys_log")
public class LogVO implements Serializable {
    //    id
    @TableId(value ="id" ,type = IdType.AUTO)
    @Excel(name = "id",orderNum = "1",type = 10)
    private Long id;
    //    用户id
    @Excel(name = "用户id",orderNum = "2")
    private Long userId;
    //用户名
    @Excel(name = "用户名称",orderNum = "3",width = 20)
    private String username;
    //    用户操作
    @Excel(name = "用户操作",orderNum = "4")
    private String operation;
    //    响应时间
    @Excel(name = "响应时间",orderNum = "5")
    private int time;
    //    请求方法
    @Excel(name = "请求方法",orderNum = "6",width = 60)
    private String method;
    //    请求参数
    @Excel(name = "请求参数",orderNum = "7",width = 20)
    private String params;
    //    IP地址
    @Excel(name = "ip地址",orderNum = "8",width = 20)
    private String ip;
    //创建时间
    @Excel(name = "创建时间",orderNum = "9",width = 20,exportFormat = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
    private Date gmtCreate;
    //    操作描述
    @Excel(name = "操作描述",orderNum = "10",width = 20)
    private String description;

## 处理方法

    @ApiOperation(value = "获取日志列表", notes = "")
    @GetMapping("/exlDownload")
    public void exlDownload(@RequestParam Map<String, String> params, HttpServletResponse response) throws UnsupportedEncodingException {
        String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String fileName = "日志记录" + date + ".xls";
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.addHeader("Content-Disposition",
                "attachment;filename=".concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8")))); // 转码之后下载的文件不会出现中文乱码
        //查询列表数据
        PageUtils pageUtils = sysLogService.list(params);
        try {
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), LogVO.class, pageUtils.getRows());
            OutputStream out = response.getOutputStream();
            workbook.write(out);
            out.close();
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
相关标签: 工作随笔 java