EasyExcel3.1.1动态为数字列设置保留两位小数不生效,有大佬能给点建议么? 以下是我的代码 public class CustomCellStyleStrategy extends AbstractCellStyleStrategy {
/**
* 通用头样式
*/
private WriteCellStyle headWriteCellStyle;
/**
* 通用内容样式
*/
private List<WriteCellStyle> contentWriteCellStyleList;
public static final Pattern pattern = Pattern.compile("^[-+]?[0-9]+\\.[0-9]+$");
public CustomCellStyleStrategy() {
this.headWriteCellStyle = getHeadStyle();
this.contentWriteCellStyleList = ListUtils.newArrayList(getContentStyle());
}
@Override
protected void setHeadCellStyle(CellWriteHandlerContext context) {
if (stopProcessing(context) || headWriteCellStyle == null) {
return;
}
WriteCellData<?> cellData = context.getFirstCellData();
WriteCellStyle.merge(headWriteCellStyle, cellData.getOrCreateStyle());
if (context.getColumnIndex() == 0) {
// 是第一列 左对齐
WriteCellStyle cellStyle = cellData.getOrCreateStyle();
cellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
}
}
@Override
protected void setContentCellStyle(CellWriteHandlerContext context) {
if (stopProcessing(context) || CollectionUtils.isEmpty(contentWriteCellStyleList)) {
return;
}
WriteCellData<?> cellData = context.getFirstCellData();
if (context.getRelativeRowIndex() == null || context.getRelativeRowIndex() <= 0) {
WriteCellStyle.merge(contentWriteCellStyleList.get(0), cellData.getOrCreateStyle());
} else {
WriteCellStyle.merge(
contentWriteCellStyleList.get(context.getRelativeRowIndex() % contentWriteCellStyleList.size()),
cellData.getOrCreateStyle());
}
if (context.getColumnIndex() == 0) {
// 是第一列 左对齐
WriteCellStyle cellStyle = cellData.getOrCreateStyle();
cellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
cellData.setWriteCellStyle(cellStyle);
}
// // 是否是小数,小数需要格式化 (打断点发现是执行了设置格式化的语句的)
String stringValue = cellData.getStringValue();
if (isDecimalPointAndNumber(stringValue)) {
cellData.setType(CellDataTypeEnum.NUMBER);
WriteCellStyle cellStyle = cellData.getOrCreateStyle();
DataFormatData dataFormatData = new DataFormatData();
dataFormatData.setFormat("0.00");
dataFormatData.setIndex((short) 2);
cellStyle.setDataFormatData(dataFormatData);
cellData.setNumberValue(new BigDecimal(stringValue));
cellData.setStringValue(null);
cellData.setWriteCellStyle(cellStyle);
}
// 下边这种方式 格式化的小数点位数不对
// Cell cell = context.getCell(); // String stringCellValue = cell.getStringCellValue(); // if (isDecimalPointAndNumber(stringCellValue)) { // // 设置小数点后的位数为2位 // CellStyle cellStyle = cell.getCellStyle(); // cellStyle.setDataFormat((short)BuiltinFormats.getBuiltinFormat("0.00")); // cell.setCellType(CellType.NUMERIC); // cell.setCellValue(Double.parseDouble(stringCellValue)); // }
}
protected boolean stopProcessing(CellWriteHandlerContext context) {
return context.getFirstCellData() == null;
}
public WriteCellStyle getHeadStyle() {
WriteCellStyle style = new WriteCellStyle();
// 背景天藍色
style.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
style.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
// 字体设置
WriteFont writeFont = new WriteFont();
writeFont.setFontName("Calibri");
// 加粗
writeFont.setBold(true);
// 字号
writeFont.setFontHeightInPoints((short) 11);
style.setWriteFont(writeFont);
// 水平居中
style.setHorizontalAlignment(HorizontalAlignment.CENTER);
// 垂直居中
style.setVerticalAlignment(VerticalAlignment.CENTER);
// 设置自动换行
style.setWrapped(true);
// 设置文本收缩至合适
style.setShrinkToFit(true);
return style;
}
public WriteCellStyle getContentStyle() {
WriteCellStyle style = new WriteCellStyle();
// 水平居中
style.setHorizontalAlignment(HorizontalAlignment.CENTER);
// 垂直居中
style.setVerticalAlignment(VerticalAlignment.CENTER);
// 设置自动换行
style.setWrapped(true);
// 设置文本收缩至合适
style.setShrinkToFit(true);
return style;
}
public static boolean isDecimalPointAndNumber(String input) {
if (StrUtil.isBlank(input)) {
return false;
}
return pattern.matcher(input).find();
}