当前位置: 首页 > article >正文

Easy Excel设置表格样式

1. 设置通用样式

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.xxx.npi.config.easypoi.EasyExcelDateConverter;
import lombok.Data;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import java.util.Date;

@Data
@HeadFontStyle(fontHeightInPoints = 11, fontName = "Calibri", color = 9)
@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 30, leftBorderColor = 22,
        rightBorderColor = 22, topBorderColor = 22, bottomBorderColor = 22)
@ContentFontStyle(fontHeightInPoints = 11, fontName = "Calibri")
@ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER, verticalAlignment = VerticalAlignment.CENTER)
@ColumnWidth(16)
@HeadRowHeight(33)
public class TransactionListVo {}

2. 指定字段样式

2.1列宽,表头名称,接口返回样式
	@ColumnWidth(value = 20)
    @ExcelProperty(value = "Transaction Date", converter = EasyExcelDateConverter.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
    private Date datetime;
2.2 日期字段,设置输出样式
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.xxx.platform.util.DateUtil;

import java.text.ParseException;
import java.util.Date;
import java.util.Objects;


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

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

    @Override
    public Date convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return parseDate(cellData);
    }

    @Override
    public CellData convertToExcelData(Date date, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return new CellData(parseString(date));
    }

    private Date parseDate(CellData cellData) throws ParseException {
        if (cellData.getType() == CellDataTypeEnum.STRING) {
            String dateSTR = cellData.getDataFormatString();
            Date date = DateUtil.stringToDate(dateSTR, DateUtil.formatStr_yyyyMMddHHmmss);
            return date;
        }
        return null;
    }

    private String parseString(Date date) throws ParseException {
        if (Objects.nonNull(date)) {
            String s = DateUtil.dateToString(date, DateUtil.formatStr_yyyyMMddHHmmss_1);
            return s;
        }
        return null;
    }

}

http://www.kler.cn/a/154002.html

相关文章:

  • Elasticsearch集群拒绝请求:索引磁盘使用超限
  • Docker 基础命令介绍和常见报错解决
  • 理解 Python 中的 __getitem__ 方法:在自定义类中启用索引和切片操作
  • 51单片机--- 矩阵按键仿真
  • .NET桌面应用架构Demo与实战|WPF+MVVM+EFCore+IOC+DI+Code First+AutoMapper
  • jmeter常用配置元件介绍总结之配置元件
  • 【Element-ui】Icon 图标与Button 按钮
  • 【代码】基于改进差分进化算法的微电网调度研究matlab
  • 6.1810: Operating System Engineering <Lab2 syscall: System calls>
  • 【MySQL】视图 + 用户管理
  • 2023 如何下载最干净的 win10 win11 微软官方原版系统镜像(详细图文)
  • VBA数据库解决方案第七讲:如何利用Recordset对象打开数据库的数据记录集
  • 新手村之SQL——增删改查条件查询
  • 2024年甘肃省职业院校技能大赛中职组 电子与信息类“网络安全”赛项竞赛样题-B卷
  • 【halcon】C# halcon 内存暴增
  • SQL优化的面试题
  • c# 责任链模式
  • 1076 Forwards on Weibo (链接表层序遍历)
  • Linux常用命令汇总
  • 【python】python列表的用法记录
  • 第15关 K8s HPA:自动水平伸缩Pod,实现弹性扩展和资源优化
  • 玄学调参实践篇 | 深度学习模型 + 预训练模型 + 大模型LLM
  • java学习part26线程安全
  • Maven终端打包时报Unknown lifecycle phase “.test.skip=true“
  • Java Servlet
  • MySQL安全相关——TDE和数据脱敏功能介绍