参考:常见api
java.text.DecimalFormat用法详解

使用场景:

比如Long 数字,身份证号码,如果直接导出excel,数字后面会变成0,这个时候需要把数字转成字符串才可以不失真。

这个注解最后还是数字格式,并不能把数字转字符串

  1. @NumberFormat(value = "#.##%")

使用自定义转换器
参考:
EasyExcel自定义转换器
@ExcelProperty(converter = TjEasyExcelLongConverter.class)

  1. package com.tj.utils.easyExcel;
  2. import com.alibaba.excel.converters.Converter;
  3. import com.alibaba.excel.metadata.GlobalConfiguration;
  4. import com.alibaba.excel.metadata.data.WriteCellData;
  5. import com.alibaba.excel.metadata.property.ExcelContentProperty;
  6. public class TjEasyExcelLongConverter implements Converter<Long> {
  7. @Override
  8. public WriteCellData<?> convertToExcelData(Long value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
  9. String res = String.valueOf(value); //把Long转成字符串
  10. return new WriteCellData<String>(res);
  11. }
  12. }