EasyExcel导出Excel-表头格式化

文章目录

  • 一、定义去除EasyExcel原生格式定义
  • 二、实体类
  • 三、设置参数
  • 四、结果展示

一、定义去除EasyExcel原生格式定义

import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.BorderStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import org.apache.poi.ss.usermodel.IndexedColors;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;


public   class excelTest{

    //设置样式 去除默认表头样式及设置内容居中
    public HorizontalCellStyleStrategy getStyleStrategy() {
        //内容样式策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //垂直居中,水平居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
        //设置 自动换行
        contentWriteCellStyle.setWrapped(true);
        // 字体策略
        WriteFont contentWriteFont = new WriteFont();
        // 字体大小
        contentWriteFont.setFontHeightInPoints((short) 12);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        //头策略使用默认
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }


}


二、实体类

package com.zdft.warehouse.prepaymentpool.model;

import java.util.Date;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.HeadStyle;
import com.alibaba.fastjson.annotation.JSONField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;


/**
 * 预付款池管理(PrepaymentPoolManage)实体类
 *
 * @author yhc
 * @since 2022-12-16 22:40:18
 */
@ApiModel("预付款池管理-FilterVoModel")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PrepaymentPoolManageFilterVoModel implements Serializable {

    /**
     * 主键
     */
    @ApiModelProperty("主键")
    @ExcelIgnore
    private Long id;

    @ApiModelProperty("序号")
    @ExcelProperty("序号")
    @ColumnWidth(10)
    private int number;

    /**
     * 供应商编码
     */
    @ApiModelProperty("value =供应商编码")
    @ExcelProperty("供应商编码")
    @ColumnWidth(25)
    private String venderCode;
    /**
     * 供应商名称
     */
    @ApiModelProperty("value =供应商名称")
    @ExcelProperty("供应商名称")
    @ColumnWidth(25)
    private String venderName;
    /**
     * 预付款总金额
     */
    @ApiModelProperty("value =预付款总金额")
    @ExcelProperty("预付款总金额")
    @ColumnWidth(25)
    private String advancePaymentAmount;
    /**
     * 已付预付款额
     */
    @ApiModelProperty("value =已付预付款额")
    @ExcelProperty("已付预付款额")
    @ColumnWidth(25)
    private String prepaymentPaid;
    /**
     * 退预付款额
     */
    @ApiModelProperty("value =退预付款额")
    @ExcelProperty("退预付款额")
    @ColumnWidth(25)
    private String refundOfAdvancePayment;
    /**
     * 可用预付款额
     */
    @ApiModelProperty("value =可用预付款额")
    @ExcelProperty("可用预付款额")
    @ColumnWidth(25)
    private String availablePrepayment;
    /**
     * 创建人
     */
    @ApiModelProperty("value =创建人")
    @ExcelIgnore
    private String createUser;
    /**
     * 创建时间
     */
    @ApiModelProperty("value =创建时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    @ExcelIgnore
    private Date createTime;
    /**
     * 更新人
     */
    @ApiModelProperty("value =更新人")
    @ExcelIgnore
    private String updateUser;
    /**
     * 更新时间
     */
    @ApiModelProperty("value =更新时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    @ExcelIgnore
    private Date updateTime;
    /**
     * 备注
     */
    @ApiModelProperty("value =备注")
    @ExcelIgnore
    private String remark;
    /**
     * 删除标记(0、未删除,1、已删除)
     */
    @ApiModelProperty("value =删除标记(0、未删除,1、已删除)")
    @ExcelIgnore
    private Integer deleted;


    @ApiModelProperty("判断明细表")
    @ExcelIgnore
    private String ifDetail;

}


三、设置参数


public  class excelTest{

public  void export(){

//第一步造数据

 List<PrepaymentPoolManageFilterVoModel> list=new ArrayList<>();
//添加数据...list.add()
 
      try {
            //第二步:设置参数
            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(), PrepaymentPoolManageFilterVoModel.class).sheet("模板").autoTrim(true).registerWriteHandler(getStyleStrategy()).doWrite(list);

        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("500", "服务器异常");
        }
}

}

四、结果展示

在这里插入图片描述

你可能感兴趣的:(技巧,excel)