Some usages of [EasyExcel]

1. AnalysisEventListener listening class, which can be used to parse Excel

It is used for monitoring. On the one hand, it can handle some data screening tasks such as checking for empty data and checking for duplicate data. On the other hand, data storage can also be done. If data storage is done here, then data conversion can also be completed here.

In this listener, the analyzed data, header information, and operation information performed after the analysis is completed are obtained by overriding the AnalysisEventListener method.

public class ExcelListener extends AnalysisEventListener<UserData> {
    
    

 /**
  * 一行一行的读取excel内容
  */
 public void invoke(UserData data, AnalysisContext analysisContext) {
    
    
  System.out.println("****" + data);
 }

 /**
  * 读取表头内容
  */
 public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
    
    
  System.out.println("表头" + headMap);
 }

 /**
  * 读取完成操作
  */
 public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    
    
  System.out.println("读取Excel完毕");
 }
}

transfer:

public class EasyExcelReadDemo {
    
    

 public static void main(String[] args) {
    
    
  // 实现excel写操作
  //1.设置写入文件夹地址和excel文件名称
  String fileName = "/Users/zzs/temp/excel/write.xlsx";
  //调用easyExcel里面的方法实现写操作
  //2个参数,第一个参数是文件名称,第二个参数是实体类
  EasyExcel.read(fileName, UserData.class, new ExcelListener()).sheet().doRead();
 }
}

2. Custom converter

Through a custom converter, for example, convert 1 and 0 into male and female instances:

public class SexConverter implements Converter<Integer> {
    
    

    public Class<Integer> supportJavaTypeKey() {
    
    
        return Integer.class;
    }

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

    public Integer convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    
    
        return "男".equals(cellData.getStringValue()) ? 1 : 0;
    }

    public CellData<String> convertToExcelData(Integer integer, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    
    
        return new CellData<>(integer.equals(1) ? "男" : "女");
    }
}

use:

@ExcelProperty(value="性别",converter = SexConverter.class)
private Integer sex;

Generate Excel again, and the gender field content will be displayed as: male and female.

3. Keep two decimal places

For example, the weight needs to be kept to two decimal places, which can be achieved through the @NumberFormat annotation:

@ExcelProperty(value = "体重KG")
@NumberFormat("0.##") // 会以字符串形式生成单元格,要计算的列不推荐
private BigDecimal weight;

Another method is to use the @ContentStyle annotation: this can also achieve the effect of retaining two decimal places.
Of course, it can also be implemented by implementing the Converter interface (same-gender implementation).

@ContentStyle(dataFormat = 2)
private BigDecimal weight2;

4. Exclude specified Excel columns

In many scenarios, Excel columns and entity classes may not be completely consistent. In this case, some fields of the entity class need to be excluded.
Method 1: Add the annotation @ExcelIgnoreUnannotated to the class, and filter the fields whose properties are not annotated with @ExcelProperty

@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor  // 一定要有无参构造方法
@ExcelIgnoreUnannotated
public class UserData {
    
    
    .....
}

Method 2: Add the @ExcelIgnore annotation to the specified field

@ExcelIgnore // 该字段不生成excel
private String remark;

Method 3: The code specifies the filter field through the excludeColumnFiledNames method. The advantage of this method is that the same Excel can exclude different data columns when calling the method.

EasyExcel.write(fileName, UserData.class).sheet("学生信息表").excludeColumnFiledNames(Arrays.asList("remark")).doWrite(getData());

Reference link: https://jsnds.cn/2021/07/29/104400.html

Guess you like

Origin blog.csdn.net/m0_46459413/article/details/128659592