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;
    }

}

原文地址:https://blog.csdn.net/leinminna/article/details/134670704

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_4819.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注