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

做了列宽的自动适配,但是@ColumnWidth失效了 #4034

Open
cxz0101 opened this issue Oct 25, 2024 · 6 comments
Open

做了列宽的自动适配,但是@ColumnWidth失效了 #4034

cxz0101 opened this issue Oct 25, 2024 · 6 comments
Assignees
Labels
help wanted Extra attention is needed

Comments

@cxz0101
Copy link

cxz0101 commented Oct 25, 2024

0fc7f0172df321bec10b07a9cb8e24d

10a4b9a6c870bae49d46aa046c15310

就是想要有指定的就按照指定长度 @ColumnWidth来,没有的话,就是自动适应,这个怎么写呢,谢谢

@cxz0101 cxz0101 added the help wanted Extra attention is needed label Oct 25, 2024
@psxjoy
Copy link
Collaborator

psxjoy commented Oct 25, 2024

The "auto-width" feature is still in the experimental phase, so we can't guarantee full compatibility with tables at the moment.

@psxjoy psxjoy self-assigned this Oct 25, 2024
@cxz0101
Copy link
Author

cxz0101 commented Oct 25, 2024

我拓展了这个代码,代码如下,想问一下,这样可以嘛,测试是没问题的,可以识别@ColumnWidth

public class CustomColumnWidthStrategy extends LongestMatchColumnWidthStyleStrategy {

private static final int MAX_COLUMN_WIDTH = 255;
private static final int MIN_COLUMN_WIDTH = 20;

@Override
protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList,
                              Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
    // 检查是否有 @ColumnWidth 注解
    ColumnWidth columnWidthAnnotation = head.getField().getAnnotation(ColumnWidth.class);
    if (columnWidthAnnotation != null) {
        // 有注解时直接按注解宽度设置列宽并返回
        int specifiedWidth = columnWidthAnnotation.value();
        writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), specifiedWidth * 256);
        return;
    }

    // 如果没有注解,则按自动宽度计算逻辑
    Integer columnWidth = dataLength(cellDataList, cell, isHead);
    if (columnWidth != null && columnWidth >= 0) {
        if (columnWidth > MAX_COLUMN_WIDTH) {
            columnWidth = MAX_COLUMN_WIDTH;
        } else if (columnWidth < MIN_COLUMN_WIDTH) {
            // 小于最小宽度时,按比例扩展
            columnWidth *= 2;
        }
        writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 256);
    }
}

private Integer dataLength(List<WriteCellData<?>> cellDataList, Cell cell, Boolean isHead) {
    if (isHead) {
        return cell.getStringCellValue().getBytes().length;
    } else {
        WriteCellData<?> cellData = (WriteCellData)cellDataList.get(0);
        CellDataTypeEnum type = cellData.getType();
        if (type == null) {
            return -1;
        } else {
            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;
            }
        }
    }
}

}

@psxjoy
Copy link
Collaborator

psxjoy commented Oct 25, 2024

我拓展了这个代码,代码如下,想问一下,这样可以嘛,测试是没问题的,可以识别@ColumnWidth

public class CustomColumnWidthStrategy extends LongestMatchColumnWidthStyleStrategy {

private static final int MAX_COLUMN_WIDTH = 255;
private static final int MIN_COLUMN_WIDTH = 20;

@Override
protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList,
                              Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
    // 检查是否有 @ColumnWidth 注解
    ColumnWidth columnWidthAnnotation = head.getField().getAnnotation(ColumnWidth.class);
    if (columnWidthAnnotation != null) {
        // 有注解时直接按注解宽度设置列宽并返回
        int specifiedWidth = columnWidthAnnotation.value();
        writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), specifiedWidth * 256);
        return;
    }

    // 如果没有注解,则按自动宽度计算逻辑
    Integer columnWidth = dataLength(cellDataList, cell, isHead);
    if (columnWidth != null && columnWidth >= 0) {
        if (columnWidth > MAX_COLUMN_WIDTH) {
            columnWidth = MAX_COLUMN_WIDTH;
        } else if (columnWidth < MIN_COLUMN_WIDTH) {
            // 小于最小宽度时,按比例扩展
            columnWidth *= 2;
        }
        writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 256);
    }
}

private Integer dataLength(List<WriteCellData<?>> cellDataList, Cell cell, Boolean isHead) {
    if (isHead) {
        return cell.getStringCellValue().getBytes().length;
    } else {
        WriteCellData<?> cellData = (WriteCellData)cellDataList.get(0);
        CellDataTypeEnum type = cellData.getType();
        if (type == null) {
            return -1;
        } else {
            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;
            }
        }
    }
}

}

Nice try. I don’t think there’s anything wrong with it, but make sure it aligns with the requirements of your business logic.

@cxz0101
Copy link
Author

cxz0101 commented Oct 25, 2024

好的,谢谢啦

@alibaba alibaba deleted a comment Oct 28, 2024
@cxz0101
Copy link
Author

cxz0101 commented Oct 29, 2024 via email

@psxjoy
Copy link
Collaborator

psxjoy commented Oct 29, 2024 via email

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

2 participants