Closed 1379734687 closed 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%")));
}
}
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); } } }
}