当前位置:   article > 正文

使用@ExcelProperty注解导出工单 excal表格

@excelproperty

展示的数据实体类

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;

@Data
@TableName("hl_hotlinemessage_hurry_do")
public class HotlinemessageHurryDo implements Serializable {


    @TableId(value = "guid")
    //excal 表格中不需要展示
    @ExcelIgnore   
    private String guid;

    /**
     * 工单号
     */
    @ExcelProperty(value = {"催办信息信息","催办单编号",.....})
    private String id;

    /**
     * 发送时间(sendTime)
     */
     // converter 转换器,导出的时候将date 类型转换
    @ExcelProperty(value = {"催办信息信息","发送时间",.....},converter = LocalDateTimeStringConverter.class)
    private Date sentTime;



}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

转换类 导出的时候将date 类型转换

public class LocalDateTimeStringConverter implements Converter<Date> {
    @Override
    public Class supportJavaTypeKey() {
        return LocalDate.class;
    }

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

    @Override
    public Date convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return DateHelper.getFormatDate(cellData.getStringValue(), "yyyy-MM-dd HH:mm:ss");
    }

    @Override
    public CellData convertToExcelData(Date date, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String format = DateHelper.formatDateTime(date);
        return new CellData(format);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

接口

@PostMapping("checkExportExcel")
    public void checkDownload(@RequestBody JSONObject params, HttpServletResponse response) {
        SystemUser currentUser = CurrentUser.getCurrentUser();
        try {
            String fileName = DateUtils.getDate("yyyyMMddhhmmss") + "工单列表.xlsx";
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
                //获取导出数据
              List<TaskhotlineExcel> taskhotlines = hlTaskhotlineService.getExportExcelList(params , currentUser);
              taskhotlines.stream().forEach(e->{
                    if(Objects.equals("1",e.getIsConfidential())){
                        e.setConsultantName("*******");
                        e.setContactAddress("*******");
                        e.setContactInformation("*******");
                    }
                });
                EasyExcel.write(response.getOutputStream(), TaskhotlineExcel.class).excelType(ExcelTypeEnum.XLSX).sheet("工单列表").doWrite(taskhotlines);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/算法构造者2/article/detail/61116
推荐阅读
相关标签
  

闽ICP备14008679号