EasyExcel 批量设置单元格样式(字体样式、底纹样式、边框样式、对齐方式、自动换行、旋转文字、竖向文字、数据格式、自动收缩)

目录

1 Maven配置

2 CellStyleModel

3 CustomCellStyleHandler

4 调试代码

5 调试结果

注:


1 Maven配置

        <!--hutool工具包-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.5.1</version>
        </dependency>
        <!-- EasyExcel文档处理工具 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.8</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

2 CellStyleModel

样式信息类。

package com.xudongbase.easyexcel.model;

import com.xudongbase.easyexcel.model.builder.CellStyleModelBuilder;
import com.xudongbase.easyexcel.model.common.SheetCellModel;
import lombok.Getter;

/**
 * 样式信息类(Builder构建者模式)
 *
 * @author xudongmaster
 */
@Getter
public class CellStyleModel extends SheetCellModel {
    /**
     * 样式信息
     */
    private final StyleModel styleModel;

    public CellStyleModel(CellStyleModelBuilder builder) {
        this.sheetName = builder.getSheetName();
        this.colIndex = builder.getColIndex();
        this.rowIndex = builder.getRowIndex();
        this.styleModel = builder.getStyleModel();
    }

    /**
     * 生成样式信息
     *
     * @param sheetName   sheet页名称
     * @param rowIndex    行号
     * @param columnIndex 列号
     * @param styleModel  样式信息
     * @return
     */ 
    public static CellStyleModel createCellStyleModel(String sheetName, int rowIndex, int columnIndex
            , StyleModel styleModel) {
        return CellStyleModelBuilder.builder(sheetName, rowIndex, columnIndex
                , styleModel).build();
    }

}

3 CustomCellStyleHandler

        自定义单元格样式处理器,可支持字体样式、底纹样式、边框样式、对齐方式、自动换行、旋转文字、竖向文字、数据格式、自动收缩。

package com.xudongbase.easyexcel.handler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.xudongbase.easyexcel.annotation.ReflectField;
import com.xudongbase.easyexcel.constant.StyleModelConstant;
import com.xudongbase.easyexcel.enums.BorderSideEnum;
import com.xudongbase.easyexcel.model.CellStyleModel;
import com.xudongbase.easyexcel.model.StyleModel;
import com.xudongbase.easyexcel.model.common.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 自定义单元格样式处理器
 * 支持字体样式、底纹信息、边框样式、对齐方式、自动换行、旋转文字、竖向文字、数据格式、自动收缩
 *
 * @author xudongmaster
 */
@Slf4j
public class CustomCellStyleHandler extends AbstractRowWriteHandler {
    /**
     * sheet页名称列表
     */
    private List<String> sheetNameList;
    /**
     * 样式信息
     */
    private Map<String, Map<Integer, List<CellStyleModel>>> sheetCellStyleMap = new HashMap<>();
    /**
     * styleModel和CellStyle映射map
     */
    private final Map<StyleModel, CellStyle> styleCellStyleMap = new HashMap<>();

    /**
     * 自定义样式适配器构造方法
     *
     * @param cellStyleList 样式信息
     */
    public CustomCellStyleHandler(List<CellStyleModel> cellStyleList) {
        if (CollUtil.isEmpty(cellStyleList)) {
            return;
        }
        Map<String, List<CellStyleModel>> allCellStyleModelMap = cellStyleList.stream().filter(x ->
                StrUtil.isNotBlank(x.getSheetName()) && x.getStyleModel() != null)
                .collect(Collectors.groupingBy(SheetModel::getSheetName));
        if (allCellStyleModelMap == null || CollUtil.isEmpty(allCellStyleModelMap.keySet())) {
            return;
        }
        sheetNameList = new ArrayList<>(allCellStyleModelMap.keySet());
        //为了方便筛选数据
        Map<Integer, List<CellStyleModel>> sheetCellStyleModelMap;
        for (String sheetName : sheetNameList) {
            sheetCellStyleModelMap = allCellStyleModelMap.get(sheetName).stream().collect(Collectors.groupingBy(SheetCellModel::getRowIndex));
            sheetCellStyleMap.put(sheetName, sheetCellStyleModelMap);
        }
    }

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row
            , Integer relativeRowIndex, Boolean isHead) {
        //跳过表头
        if (isHead) {
            return;
        }
        Sheet sheet = writeSheetHolder.getSheet();
        //不需要添加样式,或者当前sheet页不需要添加样式
        if (sheetCellStyleMap == null || CollUtil.isEmpty(sheetCellStyleMap.keySet()) || !sheetNameList.contains(sheet.getSheetName())) {
            return;
        }
        Map<Integer, List<CellStyleModel>> rowCSMMap = sheetCellStyleMap.get(sheet.getSheetName());
        //获取当前行的样式信息
        List<CellStyleModel> rowCellStyleList = rowCSMMap.get(relativeRowIndex);
        //该行不需要设置样式
        if (CollUtil.isEmpty(rowCellStyleList)) {
            return;
        }
        Cell cell;
        for (CellStyleModel cellStyleModel : rowCellStyleList) {
            //列索引
            int colIndex = cellStyleModel.getColIndex();
            //边框样式
            cell = row.getCell(colIndex) == null ? row.createCell(colIndex) : row.getCell(colIndex);
            //样式不追加,并且CellStyle也存在
            if (!cellStyleModel.getStyleModel().isAppend()
                    && styleCellStyleMap.get(cellStyleModel.getStyleModel()) != null) {
                //样式优化,styleModel对象相同时,CellStyle对象也相同
                cell.setCellStyle(styleCellStyleMap.get(cellStyleModel.getStyleModel()));
            } else {
                //设置单元格样式
                setCellStyle(cellStyleModel, cell);
            }
        }
        //删除已添加的样式信息
        rowCSMMap.remove(relativeRowIndex);
    }

    /**
     * 给单元格设置样式
     *
     * @param cellStyleModel 样式信息
     * @param cell           单元格对象
     */
    private void setCellStyle(CellStyleModel cellStyleModel, Cell cell) {
        //优先从CellStyleMap取,没有再创建
        StyleModel styleModel = cellStyleModel.getStyleModel();
        XSSFCellStyle style = styleCellStyleMap.get(styleModel) == null
                ? (XSSFCellStyle) cell.getRow().getSheet().getWorkbook().createCellStyle() : (XSSFCellStyle) styleCellStyleMap.get(styleModel);
        // 克隆出一个 style
        style.cloneStyleFrom(cell.getCellStyle());
        //可追加的CellStyle对象不放入CellStyleMap中
        if (!styleModel.isAppend()) {
            styleCellStyleMap.put(styleModel, style);
        }
        List<Field> fieldList = FieldUtils.getFieldsListWithAnnotation(StyleModel.class, ReflectField.class);
        if (CollUtil.isNotEmpty(fieldList)) {
            //使用反射给CellStyle赋值
            for (Field field : fieldList) {
                field.setAccessible(true);
                String fieldName = field.getName();
                ReflectField reflectField = field.getAnnotation(ReflectField.class);
                String invokeMethodName = reflectField.invokeMethodName();
                Object fieldValue;
                try {
                    fieldValue = field.get(styleModel);
                } catch (IllegalAccessException e) {
                    log.error("获取StyleModel的{}属性值失败:", fieldName, e);
                    continue;
                }
                //当前字段没有赋值
                if (fieldValue == null) {
                    continue;
                }
                //数据格式(从String转化为DataFormat)
                if (StrUtil.equals(StyleModelConstant.ATTR_NAME_DATA_FORMAT, fieldName)) {
                    DataFormat dataFormat = cell.getRow().getSheet().getWorkbook().createDataFormat();
                    fieldValue = dataFormat.getFormat((String) fieldValue);
                }
                ReflectUtil.invoke(style, invokeMethodName, fieldValue);
            }
        }
        //设置底纹
        if (styleModel.getCellShadingModel() != null) {
            setCellShading(style, styleModel.getCellShadingModel());
        }
        //设置字体样式
        FontModel fontModel = styleModel.getFontModel();
        if (fontModel != null) {
            setFontStyle(cell, style, fontModel);
        }
        //设置边框样式
        Map<BorderSideEnum, BorderModel> borderModelMap = styleModel.getBorderModelMap();
        if (borderModelMap != null && borderModelMap.size() > 0) {
            setBorderStyle(style, borderModelMap);
        }
        cell.setCellStyle(style);
    }

    /**
     * 设置底纹信息
     *
     * @param style            单元格样式
     * @param cellShadingModel 底纹信息
     */
    private void setCellShading(XSSFCellStyle style, CellShadingModel cellShadingModel) {
        //背景颜色
        Object backgroundColor = cellShadingModel.getBackgroundColor();
        //前景色
        Object foregroundColor = cellShadingModel.getForegroundColor();
        //底纹样式
        FillPatternType fillPatternType = cellShadingModel.getFillPattern();
        fillPatternType = fillPatternType == null ? FillPatternType.SOLID_FOREGROUND : fillPatternType;
        //背景颜色和前景色不存在时,不设置底纹
        if (backgroundColor == null && foregroundColor == null) {
            style.setFillPattern(fillPatternType);
            return;
        }
        //设置背景颜色(背景色和前景色都存在的情况)
        if (backgroundColor != null && foregroundColor != null) {
            //使用IndexedColors定义的颜色
            if (backgroundColor instanceof IndexedColors) {
                style.setFillBackgroundColor(((IndexedColors) backgroundColor).getIndex());
            }
            //使用自定义的RGB颜色
            else if (backgroundColor instanceof XSSFColor) {
                style.setFillBackgroundColor((XSSFColor) backgroundColor);
            }
        }
        //前景色不存在,设置背景色(前景色存在设置前景色)
        foregroundColor = foregroundColor == null ? backgroundColor : foregroundColor;
        //使用IndexedColors定义的颜色
        if (foregroundColor instanceof IndexedColors) {
            style.setFillForegroundColor(((IndexedColors) foregroundColor).getIndex());
        }
        //使用自定义的RGB颜色
        else if (foregroundColor instanceof XSSFColor) {
            style.setFillForegroundColor((XSSFColor) foregroundColor);
        }
        //设置底纹样式
        style.setFillPattern(fillPatternType);
    }

    /**
     * 设置字体样式
     *
     * @param cell      单元格对象
     * @param style     单元格样式
     * @param fontModel 字体信息
     */
    private void setFontStyle(Cell cell, XSSFCellStyle style, FontModel fontModel) {
        XSSFFont font;
        //样式存在字体对象时,使用原有的字体对象
        if (style.getFontIndex() != 0) {
            font = style.getFont();
        }
        //样式不存在字体对象时,创建字体对象
        else {
            font = (XSSFFont) cell.getRow().getSheet().getWorkbook().createFont();
            //默认字体为宋体
            font.setFontName("宋体");
        }
        List<Field> fieldList = FieldUtils.getFieldsListWithAnnotation(FontModel.class, ReflectField.class);
        if (CollUtil.isNotEmpty(fieldList)) {
            //使用反射给CellStyle赋值
            for (Field field : fieldList) {
                field.setAccessible(true);
                String fieldName = field.getName();
                ReflectField reflectField = field.getAnnotation(ReflectField.class);
                String invokeMethodName = reflectField.invokeMethodName();
                Object fieldValue;
                try {
                    fieldValue = field.get(fontModel);
                } catch (IllegalAccessException e) {
                    log.error("获取FontModel的{}属性值失败:", fieldName, e);
                    continue;
                }
                //当前字段没有赋值
                if (fieldValue == null) {
                    continue;
                }
                //字段值为IndexedColors时,字段值取索引值
                if (fieldValue instanceof IndexedColors) {
                    fieldValue = ((IndexedColors) fieldValue).getIndex();
                }
                ReflectUtil.invoke(font, invokeMethodName, fieldValue);
            }
        }
        style.setFont(font);
    }

    /**
     * 设置边框样式
     *
     * @param style          单元格样式
     * @param borderModelMap 边框信息map
     */
    private void setBorderStyle(XSSFCellStyle style, Map<BorderSideEnum, BorderModel> borderModelMap) {
        //设置边框线条类型和边框线条颜色
        for (Map.Entry<BorderSideEnum, BorderModel> borderModelEntry : borderModelMap.entrySet()) {
            BorderModel borderModel = borderModelEntry.getValue();
            BorderSideEnum borderSideEnum = borderModelEntry.getKey();
            //设置边框线条类型
            BorderStyle borderStyle = borderModel.getBorderStyle();
            if (borderStyle != null) {
                ReflectUtil.invoke(style, borderSideEnum.getBorderStyleMethodName(), borderStyle);
            }
            //设置边框线条颜色
            Object borderColor = borderModel.getBorderColor();
            if (borderColor != null) {
                //使用IndexedColors定义的颜色,使用索引值
                if (borderColor instanceof IndexedColors) {
                    borderColor = ((IndexedColors) borderColor).getIndex();
                }
                ReflectUtil.invoke(style, borderSideEnum.getBorderColorMethodName(), borderColor);
            }
        }
    }
}

4 调试代码

   /**
     * 测试设置自定义单元格样式(支持字体样式、底纹信息、边框样式、对齐方式、自动换行、旋转文字、竖向文字、数据格式、自动收缩)
     */
    @Test
    public void testCellStyle() {
        try {
            File file = new File("D:/easyexcel/testCellStyle.xlsx");
            FileUtil.createNewFile(file);
            //生成表格数据
            List<List<Object>> dataList = new ArrayList<>();
            dataList.add(new ArrayList<>(Arrays.asList(new Object[]{"表头11", "表头2", "表头3", "表头4"})));
            dataList.add(new ArrayList<>(Arrays.asList(new Object[]{"表头17777777777", "表头2", "表头3", "表头4444"})));
            dataList.add(new ArrayList<>(Arrays.asList(new Object[]{"表头31", "表头2", "表头3", "表头4"})));
            dataList.add(new ArrayList<>(Arrays.asList(new Object[]{11.111, 11.111, "11.111", "表头4444444444444444"})));
            //导出文件
            List<CellStyleModel> cellStyleList = new ArrayList<>();
            //设置单元格字体(黑体),单元格大小 18号,字体颜色红色,加粗,斜体,下划线,上标,删除线
            cellStyleList.add(CellStyleModel.createCellStyleModel("模板", 2, 0
                    , StyleModel.createFontStyleModel(FontModel.createFontModel("黑体", 18D, IndexedColors.RED
                            , true, true, Font.U_SINGLE, Font.SS_SUPER, true))));
            //设置单元格背景颜色
            cellStyleList.add(CellStyleModel.createCellStyleModel("模板", 0, 1
                    , StyleModel.createCellShadingStyleModel(CellShadingModel.createCellShadingModel(null, IndexedColors.BLUE, null))));
            //设置单元格底纹
            cellStyleList.add(CellStyleModel.createCellStyleModel("模板", 0, 3
                    , StyleModel.createCellShadingStyleModel(CellShadingModel.createCellShadingModel(FillPatternType.THICK_HORZ_BANDS, IndexedColors.RED, IndexedColors.BLUE))));
            //设置单元格边框类型和边框颜色
            cellStyleList.add(CellStyleModel.createCellStyleModel("模板", 0, 2
                    , StyleModel.createBorderStyleModel(BorderModel.createBorderModel(BorderStyle.DOUBLE, IndexedColors.RED))));
            //设置对齐方式
            cellStyleList.add(CellStyleModel.createCellStyleModel("模板", 0, 3
                    , StyleModel.createAlignmentStyleModel(HorizontalAlignment.RIGHT, VerticalAlignment.BOTTOM)));
            //设置自动换行
            cellStyleList.add(CellStyleModel.createCellStyleModel("模板", 1, 0
                    , StyleModel.createWrapTextStyleModel(true)));
            //设置旋转角度
            cellStyleList.add(CellStyleModel.createCellStyleModel("模板", 0, 0
                    , StyleModel.createRotationStyleModel((short) 90)));
            //设置旋转角度
            cellStyleList.add(CellStyleModel.createCellStyleModel("模板", 1, 0
                    , StyleModel.createRotationStyleModel((short) -90)));
            //设置竖向文字
            cellStyleList.add(CellStyleModel.createCellStyleModel("模板", 2, 1
                    , StyleModel.createVerticalTextStyleModel()));
            //设置数据格式
            cellStyleList.add(CellStyleModel.createCellStyleModel("模板", 3, 0
                    , StyleModel.createDataFormatStyleModel("00")));
            //设置数据格式
            cellStyleList.add(CellStyleModel.createCellStyleModel("模板", 3, 1
                    , StyleModel.createDataFormatStyleModel("00.00")));
            //设置数据格式
            cellStyleList.add(CellStyleModel.createCellStyleModel("模板", 3, 2
                    , StyleModel.createDataFormatStyleModel("00.00")));
            //设置自动收缩
            cellStyleList.add(CellStyleModel.createCellStyleModel("模板", 3, 3
                    , StyleModel.createShrinkToFitStyleModel(true)));
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            ExcelWriter excelWriter = EasyExcel.write(fileOutputStream)
                    .registerWriteHandler(new CustomCellStyleHandler(cellStyleList)).build();
            WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
            excelWriter.write(dataList, writeSheet);
            //千万别忘记finish 会帮忙关闭流
            excelWriter.finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

5 调试结果

注:

(1)有关字体样式更详细的设置请查看以下博客。

EasyExcel 设置字体样式(字体、字体大小、字体颜色、字体加粗、字体斜体、字体下划线、字体上标下标、字体删除线)https://blog.youkuaiyun.com/qq_38974638/article/details/117388442

(2)有关自定义RGB颜色的使用请查看以下博客。

 EasyExcel 单元格背景颜色、字体颜色使用2种设置颜色方法(IndexedColors中定义的颜色,自定义RGB颜色)实现https://blog.youkuaiyun.com/qq_38974638/article/details/117395831

(3)有关边框样式更详细的设置请查看以下博客。

EasyExcel 设置边框样式(线条类型和线条颜色)https://blog.youkuaiyun.com/qq_38974638/article/details/117453000

 (4)觉得这篇博客写的不错的可以前往Gitee/GitCode点个Star,源码请查看Gitee/GitCode的xudongbase项目easyexcel分支。

文件 · easyexcel · 旭东怪 / Xudongbase · GitCode主要是项目中可以用到的共通方法,现有easyexcel分支在持续更新中。 欢迎大家Star和提交Issues。 easyexcel分支:批量设置样式,批量添加批注,批量合并单元格,设置冻结行和列,设置行高列宽,隐藏行和列,绑定下拉框数据,设置水印,插入图片https://gitcode.net/qq_38974638/xudongbase/-/tree/easyexcel

nullhttps://gitee.com/xudong_master/xudongbase/tree/easyexcel/

旭东怪的个人空间_哔哩哔哩_Bilibili旭东怪,人生低谷不可怕,可怕的是坚持不到人生转折点的那一天;旭东怪的主页、动态、视频、专栏、频道、收藏、订阅等。哔哩哔哩Bilibili,你感兴趣的视频都在B站。https://space.bilibili.com/484264966?spm_id_from=333.1007.0.0 

评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值