Easy Excel设置表格样式

1. 设置通用样式

java 复制代码
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列宽,表头名称,接口返回样式
java 复制代码
	@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 日期字段,设置输出样式
java 复制代码
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;
    }

}
相关推荐
Python大数据分析@3 小时前
python操作CSV和excel,如何来做?
开发语言·python·excel
John.liu_Test4 小时前
js下载excel示例demo
前端·javascript·excel
ruleslol6 小时前
VBA02-初识宏——EXCEL录像机
excel·vba
IT铺子6 小时前
制定Excel使用规范和指导,提升数据处理的效率和准确性,减少错误和数据丢失的风险
excel
是萝卜干呀6 小时前
Backend - Python 爬取网页数据并保存在Excel文件中
python·excel·table·xlwt·爬取网页数据
神奇夜光杯12 小时前
Python酷库之旅-第三方库Pandas(202)
开发语言·人工智能·python·excel·pandas·标准库及第三方库·学习与成长
小c君tt13 小时前
MFC中Excel的导入以及使用步骤
c++·excel·mfc
一名技术极客15 小时前
Vue2 doc、excel、pdf、ppt、txt、图片以及视频等在线预览
pdf·powerpoint·excel·文件在线预览
用余生去守护15 小时前
【反射率】-- Lab 转换(excel)
excel
进击的六角龙15 小时前
Python中处理Excel的基本概念(如工作簿、工作表等)
开发语言·python·excel