EasyExcel实现下载模板

 实体类:

package com.aicut.monitor.domain;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.core.domain.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * 切刀管控台账
 * @author zhangzhi
 */
@ExcelIgnoreUnannotated
@Getter
@Setter
public class CutterControl extends BaseEntity {

    /** 主键 */
    @ExcelIgnore
    @Schema(description = "主键")
    private Long id;

    /** 工厂编码 */
    @ExcelProperty(value = "工厂编码")
    @Schema(description = "工厂编码")
    private String factoryCode;

    /** 产线编码 */
    @ExcelProperty(value = "产线编码")
    @Schema(description = "产线编码")
    private String productionLineCode;

    /** 设备编号 */
    @ExcelProperty(value = "设备编号")
    @Schema(description = "设备编号")
    private String deviceNumber;

    /** 设备名称 */
    @ExcelProperty(value = "设备名称")
    @Schema(description = "设备名称")
    private String deviceName;

    @ExcelProperty(value = "检测刀具编码")
    @Schema(description = "检测刀具编码")
    private String cutterCode;

    @ExcelProperty(value = {"换刀位置", "上刀左"},index = 5)
    @Schema(description = "换刀位置上刀左")
    private Integer toolChangePosTopLeft;

    @ExcelProperty(value = {"换刀位置", "上刀中"},index = 6)
    @Schema(description = "换刀位置上刀中")
    private Integer toolChangePosTopCenter;

    @ExcelProperty(value = {"换刀位置", "上刀右"},index = 7)
    @Schema(description = "换刀位置上刀右")
    private Integer toolChangePosTopRight;

    @ExcelProperty(value = {"换刀位置", "下刀左"},index = 8)
    @Schema(description = "换刀位置下刀左")
    private Integer toolChangePosBottomLeft;

    @ExcelProperty(value = {"换刀位置", "下刀中"},index = 9)
    @Schema(description = "换刀位置下刀中")
    private Integer toolChangePosBottomCenter;

    @ExcelProperty(value = {"换刀位置", "下刀右"},index = 10)
    @Schema(description = "换刀位置下刀右")
    private Integer toolChangePosBottomRight;

    @ExcelProperty(value = {"累计分切米数", "上刀左"},index = 11)
    @Schema(description = "累计分切米数上刀左")
    private Integer cuttingMetersTopLeft;

    @ExcelProperty(value = {"累计分切米数", "上刀中"},index = 12)
    @Schema(description = "累计分切米数上刀中")
    private Integer cuttingMetersTopCenter;

    @ExcelProperty(value = {"累计分切米数", "上刀右"},index = 13)
    @Schema(description = "累计分切米数上刀右")
    private Integer cuttingMetersTopRight;

    @ExcelProperty(value = {"累计分切米数", "下刀左"},index = 14)
    @Schema(description = "累计分切米数下刀左")
    private Integer cuttingMetersBottomLeft;

    @ExcelProperty(value = {"累计分切米数", "下刀中"},index = 15)
    @Schema(description = "累计分切米数下刀中")
    private Integer cuttingMetersBottomCenter;

    @ExcelProperty(value = {"累计分切米数", "下刀右"},index = 16)
    @Schema(description = "累计分切米数下刀右")
    private Integer cuttingMetersBottomRight;

    /** 换刀时间 */
    @JsonFormat(timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @ExcelProperty(value = "换刀时间")
    @ColumnWidth(20)
    @Schema(description = "换刀时间")
    private Date cutterChangeTime;

    /** 上刀数 */
    @ExcelProperty(value = "上刀数")
    @Schema(description = "上刀数")
    private Integer upperKnifeNumber;

    /** 下刀数 */
    @ExcelProperty(value = "下刀数")
    @Schema(description = "下刀数")
    private Integer lowerKnifeNumber;

    @ExcelProperty(value = "更换人员")
    @Schema(description = "更换人员")
    private String modifyUser;

    /** 备注 */
    @ExcelProperty(value = "备注")
    @Schema(description = "备注")
    private String remark;

}

 导出模板到网页

@PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws UnsupportedEncodingException {
        String fileName = "切刀管控台账数据模板.xlsx";
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName );
        try {
            EasyExcel.write(response.getOutputStream(),CutterControl.class)
                    .sheet("切刀管控台账数据")
                    .doWrite(new ArrayList());
        }catch (Exception e){
            log.error(e.getMessage());
        }
    }

导出结果:

 

你可能感兴趣的:(java,java,easyexcel)