EasyExcel读取和设置单元格十六进制颜色示例

概述

本文将介绍如何使用EasyExcel读取Excel文件中单元格的十六进制颜色,然后将其存储至数据库后在进行生成EasyExcel时再通过存储的十六机制值和位置信息进行还原。

EasyExcel读取单元格颜色

目前EasyExcel自己的API不支持读取单元格颜色,但是它包含了POI的API,因此我们可以借助POI的相关方法读取,**这里POI无需再次引入,EasyExcel 中已经包含了我们所需要的方法。**实现代码如下:

/**
   * 解析单元格颜色工具类
   * @param configDescribeId 配置描述ID,本人业务需要,不是必须
   * @param file 上传文件
*/
public static List<CustomCellStyle> parseCellFillColor(String configDescribeId, MultipartFile file) {
        List<CustomCellStyle> list = new ArrayList<>();
        try(XSSFWorkbook xssfWorkbook = new XSSFWorkbook(file.getInputStream())){
            for(int sheetNum = 0; sheetNum < xssfWorkbook.getNumberOfSheets(); sheetNum++) {
                XSSFSheet sheetAt = xssfWorkbook.getSheetAt(sheetNum);
                if(sheetAt == null) {
                    continue;
                }
                XSSFRow headRow = sheetAt.getRow(1);	//拿到第一行数据,读取业务需要,可忽略和修改
                double numericCellValue = headRow.getCell(5).getNumericCellValue();	//读取第一行数据的索引为5的数据,因为我的业务中这个单元格个是有效行数,业务需要,可忽略
                int len = (int) (numericCellValue+5);
                for(int rowNum = 0; rowNum <= len; rowNum++) {
                    if(rowNum > 5) {
                        XSSFRow xssfRow = sheetAt.getRow(rowNum);
                        if(xssfRow != null) {
                            for(int colNum = 0; colNum < xssfRow.getLastCellNum(); colNum++) {
                                XSSFCell cell = xssfRow.getCell(colNum);
                                if(cell != null) {
                                    cell.setCellType(CellType.STRING);
                                    String stringCellValue = cell.getStringCellValue();
                                    XSSFCellStyle cellStyle = cell.getCellStyle();
                                    XSSFColor xssfColor = cellStyle.getFillForegroundColorColor();
                                    byte[]rgbs;
                                    if(xssfColor != null && StringUtils.isNotEmpty(stringCellValue)) {
                                        rgbs = xssfColor.getRGB();
                                        String format = String.format("#%02X%02X%02X", rgbs[0], rgbs[1], rgbs[2]);
                                        list.add(new CustomCellStyle(rowNum, colNum, configDescribeId, sheetAt.getSheetName(), format));
                                    }
                                }

                            }
                        }
                    }
                }
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

代码说明

上述代码中大致分为如下几步:

  1. 获取到XSSFWorkbook对象,通过它遍历Sheet页
  2. 遍历Sheet页获取到XSSFSheet对象
  3. 然后遍历行和列,将内容不为空并且颜色对象不为空的单元格的颜色和行索引和列索引进行存储
    主要是这部分代码:
if(xssfColor != null && StringUtils.isNotEmpty(stringCellValue)) {
 	rgbs = xssfColor.getRGB();
    String format = String.format("#%02X%02X%02X", rgbs[0], rgbs[1], rgbs[2]);	//将rgb byte[] 转换为16进制
    list.add(new CustomCellStyle(rowNum, colNum, configDescribeId, sheetAt.getSheetName(), format));
}

经过上面的方法我们就可以解析到16进制的颜色了。下面来看一下如何进行设置16进制颜色。

EasyExcel设置十六进制颜色

由于EasyExcel自己的API中没有暴露设置十六进制颜色的方法,因此我们可以使用POI的方法进行操作。我们需要实现一个EasyExcel的接口CellWriteHandler,然后在这里面操作,代码如下:

@Slf4j
public class CustomCellWriteHandler implements CellWriteHandler {
    private final List<CustomCellStyle> customCellStyleList;	//传入上面我们保存的单元格颜色对象列表

    public CustomCellWriteHandler(List<CustomCellStyle> customCellStyleList){
        this.customCellStyleList = customCellStyleList;
    }


    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
        Sheet sheet = writeSheetHolder.getSheet();
    	//以下为设置颜色的代码
        if (!customCellStyleList.isEmpty()) {
            int rowIndex = cell.getRowIndex();
            int columnIndex = cell.getColumnIndex();
            List<CustomCellStyle> tempList = customCellStyleList.stream().filter(item -> item.getX() == rowIndex && item.getY() == columnIndex && Objects.equals(item.getSheetName(), sheet.getSheetName())).collect(Collectors.toList());
            if (!tempList.isEmpty()) {
                XSSFCellStyle cellStyle = (XSSFCellStyle) sheet.getWorkbook().createCellStyle();
                Color color = Color.decode(tempList.get(0).getColor());
                cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                cellStyle.setFillForegroundColor(new XSSFColor(color));
                cell.setCellStyle(cellStyle);
            }
        }
    }

上面代码中主要是最里面的if语句包含的部分,需要注意的是**应该使用XSSFCellStyle,不能使用EasyExcel的CellStyle,这里容易出错,因为获取CellStyle的方式和XSSFCellStyle的一样,只不过加了强转。**然后创建一个Color实例,创建时通过Color的decode方法,这里的参数可以是"#FF0000"这种带#号的方式,也可以是"0xFF0000"这种方式。然后设置颜色填充模式,这一步必须设置,最后将颜色设置进去就好。

以上就是通过EasyExcel读取和设置单元格颜色的方法,本人在做的时候翻了EasyExcle文档很久没找到设置十六进制的现成方式,最后通过这种方式实现,希望能帮助到大家,如果有更好的方式可以在评论区发表以下,谢谢!😁😁😁

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@胡海龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值