alibaba/easyexcel

CellStyle设置主动换行后,变成表头样式,单元格不生效

Open

#3837 aperta il 17 giu 2024

Vedi su GitHub
 (1 commento) (0 reazioni) (0 assegnatari)Java (7599 fork)batch import
help wanted

Metriche repository

Star
 (33.728 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

异常代码

@Override
protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell,
                              Head head,
                              Integer relativeRowIndex, Boolean 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;
        CellStyle cellStyle = cell.getCellStyle();
        if (cellStyle != null) {
            cellStyle.setWrapText(true);
        }
    }
    Integer maxColumnWidth = maxColumnWidthMap.get(cell.getColumnIndex());
    if (maxColumnWidth == null || columnWidth > maxColumnWidth) {
        maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth);
        writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 256);
    }
}

问题描述

1、这段代码是重写了LongestMatchColumnWidthStyleStrategy方法 2、为了自定义最大宽度,且当达到最大宽度时,单元格设置自动换行 3、debug调试过,确定到了具体的cell是执行了cellStyle.setWrapText(true); 4、实际下载的excel中变成表头有自动换行,目标单元格没有

Guida contributor