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<CutterControl>());
        }catch (Exception e){
            log.error(e.getMessage());
        }
    }

导出结果:

 

  • 12
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用EasyExcel实现模板导出可以分为以下几个步骤: 1. 引入EasyExcel依赖:在项目的pom.xml文件中添加EasyExcel的依赖,例如: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.10</version> </dependency> ``` 2. 创建模板文件:根据需要导出的数据格式,创建一个Excel模板文件。可以在Excel中设置好表头、样式等内容。 3. 编写导出代码:在Java代码中使用EasyExcel提供的API,读取模板文件,并按照需要填充数据和样式。 ```java // 导入相关类 import com.alibaba.excel.EasyExcel; import com.alibaba.excel.write.builder.ExcelWriterBuilder; import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder; import com.alibaba.excel.write.metadata.WriteSheet; // 读取模板文件 String templateFile = "path/to/template.xlsx"; ExcelWriterBuilder excelWriterBuilder = EasyExcel.write(outputStream).withTemplate(templateFile); ExcelWriterSheetBuilder excelWriterSheetBuilder = excelWriterBuilder.sheet(); WriteSheet writeSheet = excelWriterSheetBuilder.build(); // 填充数据 List<DataObject> dataList = getData(); // 获取需要导出的数据 writeSheet.setClazz(DataObject.class); // 指定数据对象的类型 excelWriterSheetBuilder.doFill(dataList, writeSheet); // 关闭写入流 excelWriterBuilder.finish(); ``` 其中,`DataObject`是需要导出的数据对象的类,`getData()`方法用于获取需要导出的数据。 4. 导出数据:最后,将填充好数据的Excel文件输出到指定的位置。 ```java outputStream = new FileOutputStream("path/to/output.xlsx"); excelWriterBuilder.file(outputStream).build(); ``` 以上就是使用EasyExcel实现模板导出的基本步骤。根据实际需求,你可以根据EasyExcel提供的API进行更加复杂和灵活的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

顾十方

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值