EasyExcel给指定单元格加样式(CellWriteHandler版)

依赖

        <!-- easyexcel依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.1.3</version>
        </dependency>

代码

package com.szc.computing.MechanicalCalculationService.util;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;

import java.util.List;

/**
 * 将全表符合要求的(是公式)的设为公式类型(只要是以等于号开头)
 * @author zhj
 */

public class Table1StyleUtil implements CellWriteHandler {
    public Table1StyleUtil() {
    }

    @Override
    public void beforeCellCreate(CellWriteHandlerContext context) {
        CellWriteHandler.super.beforeCellCreate(context);
    }

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
        CellWriteHandler.super.beforeCellCreate(writeSheetHolder, writeTableHolder, row, head, columnIndex, relativeRowIndex, isHead);
    }

    @Override
    public void afterCellCreate(CellWriteHandlerContext context) {
        CellWriteHandler.super.afterCellCreate(context);
    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        CellWriteHandler.super.afterCellCreate(writeSheetHolder, writeTableHolder, cell, head, relativeRowIndex, isHead);
    }

    @Override
    public void afterCellDataConverted(CellWriteHandlerContext context) {
        CellWriteHandler.super.afterCellDataConverted(context);
    }

    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, WriteCellData<?> cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        CellWriteHandler.super.afterCellDataConverted(writeSheetHolder, writeTableHolder, cellData, cell, head, relativeRowIndex, isHead);
    }

    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
//        CellWriteHandler.super.afterCellDispose(context);
        // 当前事件会在 数据设置到poi的cell里面才会回调
        // 判断不是头的情况 如果是fill 的情况 这里会==null 所以用not true
        if (BooleanUtils.isNotTrue(context.getHead())) {
            Cell cell = context.getCell();
            if(cell.getColumnIndex()==17&&cell.getStringCellValue().equals("调整")){
                // 第一个单元格
                // 只要不是头 一定会有数据 当然fill的情况 可能要context.getCellDataList() ,这个需要看模板,因为一个单元格会有多个 WriteCellData
                WriteCellData<?> cellData = context.getFirstCellData();
                // 这里需要去cellData 获取样式
                // 很重要的一个原因是 WriteCellStyle 和 dataFormatData绑定的 简单的说 比如你加了 DateTimeFormat
                // ,已经将writeCellStyle里面的dataFormatData 改了 如果你自己new了一个WriteCellStyle,可能注解的样式就失效了
                // 然后 getOrCreateStyle 用于返回一个样式,如果为空,则创建一个后返回
                WriteCellStyle writeCellStyle = cellData.getOrCreateStyle();
                writeCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
                // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND
                writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);

                // 这样样式就设置好了 后面有个FillStyleCellWriteHandler 默认会将 WriteCellStyle 设置到 cell里面去 所以可以不用管了
            }

        }
    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
//            CellType cellType = cell.getCellType();
//            if(CellType.STRING==cellType){
//                StringBuffer stringBuffer =  new StringBuffer(cell.getStringCellValue());
//                if(stringBuffer.length()>=1){
//                    char c = stringBuffer.charAt(0);
//                    if (c=='='){
//                        cell.setCellFormula( stringBuffer.replace(0,1,"").toString());
//                    }
//                }
//
//            }

//            j
            if(cell.getColumnIndex()==8){
                if(cell.getCellType()==CellType.BOOLEAN){
                    if(cell.getBooleanCellValue()==true){
                        cell.setCellType(CellType.STRING);
                        cell.setCellValue("推荐");
                    }else{
                        cell.setCellType(CellType.STRING);
                        cell.setCellValue("不推荐");
                    }

                }
            }
//            0设置true为推荐,false为不推荐
        if(cell.getColumnIndex()==14){
            if(cell.getCellType()==CellType.BOOLEAN){
                if(cell.getBooleanCellValue()==true){
                    cell.setCellType(CellType.STRING);
                    cell.setCellValue("推荐");
                }else{
                    cell.setCellType(CellType.STRING);
                    cell.setCellValue("不推荐");
                }

            }
        }


    }

    @Override
    public int order() {
        return CellWriteHandler.super.order();
    }
}

关键代码

 @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
//        CellWriteHandler.super.afterCellDispose(context);
        // 当前事件会在 数据设置到poi的cell里面才会回调
        // 判断不是头的情况 如果是fill 的情况 这里会==null 所以用not true
        if (BooleanUtils.isNotTrue(context.getHead())) {
            Cell cell = context.getCell();
            if(cell.getColumnIndex()==17&&cell.getStringCellValue().equals("调整")){
                // 第一个单元格
                // 只要不是头 一定会有数据 当然fill的情况 可能要context.getCellDataList() ,这个需要看模板,因为一个单元格会有多个 WriteCellData
                WriteCellData<?> cellData = context.getFirstCellData();
                // 这里需要去cellData 获取样式
                // 很重要的一个原因是 WriteCellStyle 和 dataFormatData绑定的 简单的说 比如你加了 DateTimeFormat
                // ,已经将writeCellStyle里面的dataFormatData 改了 如果你自己new了一个WriteCellStyle,可能注解的样式就失效了
                // 然后 getOrCreateStyle 用于返回一个样式,如果为空,则创建一个后返回
                WriteCellStyle writeCellStyle = cellData.getOrCreateStyle();
                writeCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
                // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND
                writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);

                // 这样样式就设置好了 后面有个FillStyleCellWriteHandler 默认会将 WriteCellStyle 设置到 cell里面去 所以可以不用管了
            }

        }
    }

冲突问题

问题出现的背景

上面这几行代码很好从网上找到但是我在实际使用时我的项目中使用了EasyExcel的同时还用到了EasyPoi导致数据丢失、代码不起效果等功能

问题出现的原因

由于 EasyExcel与EasyPoi都是对poi的封装,我个人感觉是他俩底层用的poi版本不一致才导致了问题的出现

解决问题

依赖(将poi中的冲突的依赖剔除掉)

    <!-- easyexcel依赖 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>easyexcel</artifactId>
        <version>3.1.3</version>
    </dependency>
        <!--     easypoi依赖 -->
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-base</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml-schemas</artifactId>
            </exclusion>
        </exclusions>
        <version>4.1.3</version>
    </dependency>

这其实只是一种方法,另一种是去掉EasyExcel,EasyPoi中的poi依赖单独引入poi依赖。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值