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

属性值为null的时候不调用converter #252

Open
souldian opened this issue Mar 6, 2025 · 2 comments
Open

属性值为null的时候不调用converter #252

souldian opened this issue Mar 6, 2025 · 2 comments

Comments

@souldian
Copy link

souldian commented Mar 6, 2025

Image

@psxjoy
Copy link
Member

psxjoy commented Mar 6, 2025

I'm unable to replicate the problem you've described. Offering a minimal reproducible demo would be an excellent approach.

@souldian
Copy link
Author

souldian commented Mar 6, 2025

`public class ExportExcelTest {

public static void main(String[] args) {
    ExcelCell cell = new ExcelCell();
    cell.setName("测试");
    List<ExcelCell> cells = new ArrayList<>();
    cells.add(cell);
    EasyExcel.write(new File("D:\\test11.xlsx"), ExcelCell.class).sheet("模板").doWrite(cells);
}

@Getter
@Setter
@ExcelIgnoreUnannotated
public static class ExcelCell {
    @ExcelProperty(value = "姓名")
    private String name;

    /**
     * 期望null或者false的时候打印出否 true的时候打印出是
     */
    @ExcelProperty(value = "是否通过", converter = BaseConverter.class)
    private Boolean isPass;

}

public static class BaseConverter implements Converter<Boolean> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return Boolean.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Boolean convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
                                     GlobalConfiguration globalConfiguration) {
        return Boolean.valueOf(cellData.getStringValue());
    }

    @Override
    public WriteCellData<?> convertToExcelData(Boolean value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {

        String str = "否";
        if (Boolean.TRUE.equals(value)){
            str = "是";
        }
        if (Boolean.FALSE.equals(value)){
            str = "否";
        }

        if (Objects.isNull(value)) {
            str = "否";
        }
        return new WriteCellData<>(str);
    }
}

}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants