alibaba / easyexcel

快速、简洁、解决大文件内存溢出的java处理Excel工具
https://easyexcel.opensource.alibaba.com
Apache License 2.0
32.09k stars 7.5k forks source link

设置导出的百分数为数字格式,并且数据内容有百分号。但是导出的内容是空文档 #3871

Closed 1379734687 closed 2 months ago

1379734687 commented 2 months ago

public class ExcelNumberFormatHandler implements CellWriteHandler { @Override public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, WriteCellData<?> cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { String stringValue = cellData.getStringValue(); if (!isHead) { // 数字 if (stringValue.matches(“"^-?\d+(\.\d+)?$"”)) { WriteCellStyle writeCellStyle = new WriteCellStyle(); DataFormatData dataFormatData = new DataFormatData(); dataFormatData.setIndex(HSSFDataFormat.getBuiltinFormat("0.0000")); writeCellStyle.setDataFormatData(dataFormatData); cellData.setNumberValue(new BigDecimal(stringValue)); cellData.setType(CellDataTypeEnum.NUMBER); cellData.setWriteCellStyle(writeCellStyle); } // 百分比 if (stringValue.matches(“"^-?\d+(\.\d+)?%$"”)) { WriteCellStyle writeCellStyle = new WriteCellStyle(); DataFormatData dataFormatData = new DataFormatData(); dataFormatData.setIndex(HSSFDataFormat.getBuiltinFormat("0.00%")); writeCellStyle.setDataFormatData(dataFormatData); cellData.setNumberValue(new BigDecimal(stringValue.replace("%", ""))); cellData.setType(CellDataTypeEnum.NUMBER); cellData.setWriteCellStyle(writeCellStyle); } } }

}

frankggyy commented 2 months ago

可以正常导出,验证代码如下:

TestData.java

@Data
@Accessors(chain = true)
public class TestData {
    @ExcelProperty("数字列")
    private String data;
}

ExcelNumberFormatHandler.java

public class ExcelNumberFormatHandler implements CellWriteHandler {
    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, WriteCellData<?> cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        CellWriteHandler.super.afterCellDataConverted(writeSheetHolder, writeTableHolder, cellData, cell, head, relativeRowIndex, isHead);
        String stringValue = cellData.getStringValue();
        if (!isHead) {
            // 数字
            if (stringValue.matches("^-?\\d+(\\.\\d+)?$")) {
                WriteCellStyle writeCellStyle = new WriteCellStyle();
                DataFormatData dataFormatData = new DataFormatData();
                dataFormatData.setIndex(HSSFDataFormat.getBuiltinFormat("0.0000"));
                writeCellStyle.setDataFormatData(dataFormatData);
                cellData.setNumberValue(new BigDecimal(stringValue));
                cellData.setType(CellDataTypeEnum.NUMBER);
                cellData.setWriteCellStyle(writeCellStyle);
            }
            // 百分比
            if (stringValue.matches("^-?\\d+(\\.\\d+)?%$")) {
                WriteCellStyle writeCellStyle = new WriteCellStyle();
                DataFormatData dataFormatData = new DataFormatData();
                dataFormatData.setIndex(HSSFDataFormat.getBuiltinFormat("0.00%"));
                writeCellStyle.setDataFormatData(dataFormatData);
                cellData.setNumberValue(new BigDecimal(stringValue.replace("%", "")));
                cellData.setType(CellDataTypeEnum.NUMBER);
                cellData.setWriteCellStyle(writeCellStyle);
            }
        }
    }
}

ExcelNumberFormatHandlerTest.java

class ExcelNumberFormatHandlerTest {

    @Test
    public void write() {
        EasyExcel.write(TestFileUtil.getPath() + "ExcelNumberFormatHandlerTest.xls", TestData.class)
            .sheet()
            .registerWriteHandler(new ExcelNumberFormatHandler())
            .doWrite(ListUtils.newArrayList(new TestData().setData("0.10"), new TestData().setData("0.10%")));
    }
}

ExcelNumberFormatHandlerTest.xls