[alibaba/easyexcel]添加自定义样式注解时会应用到所有列

2024-05-10 14 views
9

触发场景描述 在某一列头添加自定义样式注解@HeadFontStyle(color = Font.COLOR_RED) 结果应用到了所有列头上

触发Bug的代码

@HeadFontStyle(color = Font.COLOR_RED)

提示的异常或者没有达到的效果 所有列头都变成了红色

回答

8

@Target({ElementType.FIELD, ElementType.TYPE}) public @interface HeadFontStyle

1

没有复现该问题,能提供一个可复现的demo吗?

5

我是自定义其他excel数据内容正常使用,很灵活就可以达到自定义目的,但判断的行列在head上时,就会出现head全部应用一模一样样式的问题, 版本为3.1.2 采用的是CellWriteHandler的方法,修改样式部分代码如下(kotlin):

override fun afterCellDispose(
        context: CellWriteHandlerContext
    ) {
        val cell = context.cell
        val writeSheetHolder = context.writeSheetHolder

        if (cell != null && writeSheetHolder != null) {
//其他代码           
...
            //自定义样式
            val headerSumRow = 4
            customStyle(context, headerSumRow..dataSize + headerSumRow, 0..headLength) {
//                it.fillForegroundColor = IndexedColors.RED.index
//                it.fillPatternType = FillPatternType.SOLID_FOREGROUND
                it.borderBottom = BorderStyle.THIN
                it.borderTop = BorderStyle.THIN
                it.borderLeft = BorderStyle.THIN
                it.borderRight = BorderStyle.THIN
            }

//此处想把head中第一个位置的单元格设置为红色
            customStyle(context,0,0) {
                it.writeFont.color = IndexedColors.RED.index
            }
        }
    }

    private fun customStyle(
        context: CellWriteHandlerContext,
        rowIndexRange: IntRange,
        columnIndexRange: IntRange,
        styleCustom: (style: WriteCellStyle) -> Unit
    ) {
        val cell = context.cell
        if (cell.rowIndex in rowIndexRange && cell.columnIndex in columnIndexRange) {
            val cellData = context.firstCellData
            styleCustom.invoke(cellData.orCreateStyle)
        }
    }

    private fun customStyle(
        context: CellWriteHandlerContext,
        rowIndex: Int,
        columnIndex: Int,
        styleCustom: (style: WriteCellStyle) -> Unit
    ) {
        customStyle(
            context = context,
            rowIndexRange = rowIndex..rowIndex,
            columnIndexRange = columnIndex..columnIndex,
            styleCustom = styleCustom
        )
    }
8

今天遇到了一样的问题

3
3003