[alibaba/easyexcel]如何设置行样式和单元格样式

2024-05-24 67 views
2

异常代码

   这里写你的代码

异常提示 请提供完整的异常提示,记住是全部异常! 建议描述 大佬您好,请我一下如何设置行样式。场景1:交易的付款订单跟退款订单都在同一个报表内,如果是退款类型的订单需要显示为红色如何处理(整行);场景2:如何将交易订单金额大于200的付款金额列显示为红色(某个特殊条件的单元格)。

回答

5

参考官方demo-自定义拦截器中的CellWriteHandler 这里的单元格已经写入完成了,但是是复用的整体的单元格,所以你需要创建一个新的(或者两个)红色单元格样式然后缓存它设置到单元格中 输出的数据也可以传进来用来判断是否需要判断,参考代码实现

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        // 这里可以对cell进行任何操作
        LOGGER.info("第{}行,第{}列写入完成。", cell.getRowIndex(), cell.getColumnIndex());
        //写入的行的model数据
        final DemoData demoData = arrayList.get(cell.getRowIndex());
        //具体业务逻辑判断
        if (demoData.getIsActive()){
            //样式一定要缓存,excel样式有上限
            cell.setCellStyle(getCellStyle(writeSheetHolder.getParentWriteWorkbookHolder().getWorkbook()));
        }
    }
7

public class DetailNormalCellStyleStrategy extends AbstractVerticalCellStyleStrategy {

private DetailContext detailContext;

private static final int MAX_COLUMN_WIDTH = 255;

private final Map<Integer, Map<Integer, Integer>> CACHE = new HashMap<Integer, Map<Integer, Integer>>(8);

public DetailNormalCellStyleStrategy(DetailContext detailContext) {
    this.detailContext = detailContext;
}

@Override
protected WriteCellStyle headCellStyle(Head head) {
    WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
    contentWriteCellStyle.setLocked(true);
    WriteFont contentWriteFont = new WriteFont();
    contentWriteFont.setFontHeightInPoints((short)22);
    contentWriteCellStyle.setWriteFont(contentWriteFont);
    return contentWriteCellStyle;
}

@Override
protected WriteCellStyle contentCellStyle(Head head) {
    int columnIndex = head.getColumnIndex();
    String headCode = detailContext.getHeadCodes().get(columnIndex).get(0);
    DetailShow detailShow = detailContext.getProgram().getDetailHeadShow().getShowByHeadCode(headCode);
    WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
    WriteFont contentWriteFont = new WriteFont();
    contentWriteFont.setFontHeightInPoints((short)20);
    contentWriteFont.setColor(detailShow.getColor().index());
    contentWriteCellStyle.setWriteFont(contentWriteFont);
    return contentWriteCellStyle;
}

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

@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
    super.afterCellDispose(writeSheetHolder, writeTableHolder, cellDataList, cell, head, relativeRowIndex, isHead);
    boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList);
    if (!needSetWidth) {
        return;
    }
    Map<Integer, Integer> maxColumnWidthMap = CACHE.get(writeSheetHolder.getSheetNo());
    if (maxColumnWidthMap == null) {
        maxColumnWidthMap = new HashMap<Integer, Integer>(16);
        CACHE.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap);
    }
    Integer columnWidth = dataLength(cellDataList, cell, isHead);
    if (columnWidth < 0) {
        return;
    }
    if (columnWidth > MAX_COLUMN_WIDTH) {
        columnWidth = MAX_COLUMN_WIDTH;
    }
    Integer maxColumnWidth = maxColumnWidthMap.get(cell.getColumnIndex());
    if (maxColumnWidth == null || columnWidth > maxColumnWidth) {
        maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth);
        writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 256);
    }
}

private Integer dataLength(List<CellData> cellDataList, Cell cell, Boolean isHead) {
    if (isHead) {
        return cell.getStringCellValue().getBytes().length;
    }
    CellData cellData = cellDataList.get(0);
    CellDataTypeEnum type = cellData.getType();
    if (type == null) {
        return -1;
    }
    switch (type) {
        case STRING:
            return cellData.getStringValue().getBytes().length;
        case BOOLEAN:
            return cellData.getBooleanValue().toString().getBytes().length;
        case NUMBER:
            return cellData.getNumberValue().toString().getBytes().length;
        default:
            return -1;
    }
}

} 您好,上面是我的样式代码。我在afterCellDispose方法处进行断点测试,List cellDataList的大小为1,但是为里面对象null,以至于我得不到数据,做不了具体的格式转换,是什么原因呢

3

您好,之前应该是我理解错了,分析下了,应该可以支持到行和单元格的,谢谢大佬的解答。