Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jakesang opened this issue Jun 17, 2024 · 0 comments
Open
Labels
help wanted Extra attention is needed

Comments

@jakesang
Copy link

异常代码

@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中变成表头有自动换行,目标单元格没有

@jakesang jakesang added the help wanted Extra attention is needed label Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant