Java+vue实现数据导出为excel表格(EasyExcel)

1.实体类

注意:实体类中涉及到的状态如果是用Integer类型的的数字表示,那么需要用到转换器来转换在这里我的状态是用0表示为"待维修"状态。

@ExcelProperty:设置表头
@ExcelIgnore:忽略不需要的字段(不会再表格中显示)
 package com.wedu.modules.eq.pojo;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.wedu.modules.eq.Converter.EquipmentMStatusConverter;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * @TableName eq_wait_repair
 */
@TableName(value ="eq_wait_repair")
@Data
public class WaitRepair implements Serializable {

    @ExcelProperty("序号")
    private Integer id;

    @ExcelProperty("设备编号")
    private String equipmentId;

    @TableField(exist = false)
    @ExcelIgnore
    private String equipmentName;

    @TableField(exist = false)
    @ExcelIgnore
    private String equipmentModel;

    @ExcelProperty("红灯开始时间")
    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    private Date redlightBegintime;

    //设置转换器
    @ExcelProperty(value = "状态", converter = EquipmentMStatusConverter.class)
    private Integer status;

    @ExcelIgnore
    private String createUser;

    private Date createTime;

    @ExcelIgnore
    private String updateUser;

    private Date updateTime;

    @ExcelIgnore
    private Integer isDeleted;

    private static final long serialVersionUID = 1L;
}

2.转换器(可在实体类的@ExcelProperty注解中使用)

package com.wedu.modules.eq.Converter.Enum;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.stream.Stream;

@Getter
@AllArgsConstructor
public enum EquipmentMEnum {

    NORMAL(3,"待维修"),

    WARNING(0,"已维修");
    
    private final Integer value;

    @JsonFormat
    private final String description;

    public static EquipmentMEnum convert(Integer value) {
        return Stream.of(values())
                .filter(bean -> bean.value.equals(value))
                .findAny()
                .orElse(NORMAL);
    }

    public static EquipmentMEnum convert(String description) {
        return Stream.of(values())
                .filter(bean -> bean.description.equals(description))
                .findAny()
                .orElse(NORMAL);
    }

}
package com.wedu.modules.eq.Converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.wedu.modules.eq.Converter.Enum.EquipmentMEnum;

public class EquipmentMStatusConverter implements Converter<Integer> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Integer convertToJavaData(ReadConverterContext<?> context) {
        return EquipmentMEnum.convert(context.getReadCellData().getStringValue()).getValue();
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Integer> context) {
        return new WriteCellData<>(EquipmentMEnum.convert(context.getValue()).getDescription());
    }


}

3.Controller层

以下代码来自EasyExcel官方文档 - 基于Java的Excel处理工具 | Easy Excel 官网

  @GetMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), WaitRepair.class).sheet("模板").doWrite(waitRepairService.list());
    }

4.vue代码

 <el-button type="success" @click="toexcel()">导出excel</el-button>
 toexcel(){
          window.location.href =this.$http.adornUrl(`/eq/waitRepair/download?token=${this.$cookie.get('token')}`)
        },

注意:methods中的接口路径要和后端Controller层一致

5.效果展示

虽然在我的项目上实现了,但是可能还存在不完善的地方,各位使用时请注意!

以上代码是我在一个完整的项目中截取的只涉及到数据导出为excel表格的功能!!!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值