EasyExcel 自定义类型转换器Converter3种加载方式(转换字段加载类型转换器、导出时加载类型转换器、加载全局类型转换器)

1 Maven依赖

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.8</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

2 转换字段加载类型转换器

2.1 @ExcelProperty的converter属性加载类型转换器

package com.dto;

import com.alibaba.excel.annotation.ExcelProperty;
import com.converter.LocalDateConverter;
import lombok.Data;

import java.time.LocalDate;

@Data
public class TestDto {
    @ExcelProperty(value = "创建时间", index = 1, converter = LocalDateConverter.class)
    private LocalDate createDate;
}

2.2 调试代码

    /**
     * 字段加载类型转换器
     *
     * @param response
     */
    @GetMapping("/attributeConverter")
    public void attributeConverter(HttpServletResponse response) throws IOException {
        List<TestDto> sheetDataList = new ArrayList<>();
        TestDto testDto=new TestDto();
        testDto.setCreateDate(LocalDate.parse("2021-07-30", DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        sheetDataList.add(testDto);
        response.addHeader("Content-Disposition", "filename=test.xlsx");
        //设置类型,扩展名为.xls
        response.setContentType("application/vnd.ms-excel");
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), TestDto.class).build();
        WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
        excelWriter.write(sheetDataList, writeSheet);
        //千万别忘记finish 会帮忙关闭流
        excelWriter.finish();
    }

2.3 调试结果

3 导出时加载类型转换器

3.1 调试代码

    /**
     * 导出时加载类型转换器
     *
     * @param response
     */
    @GetMapping("/exportRegisterConverter")
    public void exportRegisterConverter(HttpServletResponse response) throws IOException {
        List<List<Object>> sheetDataList = new ArrayList<>();
        List<Object> rowList = new ArrayList<>();
        rowList.add(LocalDate.parse("2021-07-30", DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        sheetDataList.add(rowList);
        response.addHeader("Content-Disposition", "filename=test.xlsx");
        //设置类型,扩展名为.xls
        response.setContentType("application/vnd.ms-excel");
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
                //加载类型转换器
                .registerConverter(new LocalDateConverter()).build();
        WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
        excelWriter.write(sheetDataList, writeSheet);
        //千万别忘记finish 会帮忙关闭流
        excelWriter.finish();
    }

3.2 调试结果

4 加载全局类型转换器

4.1 CustomConverterLoader

加载自定义类型转换器(加载器)。

package com.loader;

import com.alibaba.excel.converters.*;
import com.converter.LocalDateConverter;
import org.springframework.context.annotation.*;

import java.util.Map;

/**
 * 加载自定义类型转换器(加载器)
 */
@Configuration
public class CustomConverterLoader {

    @Bean
    public DefaultConverterLoader defaultConverterLoader() {
        DefaultConverterLoader converters = new DefaultConverterLoader();
        LocalDateConverter localDateConverter = new LocalDateConverter();
        Map<String, Converter> defaultWriteConverter = converters.loadDefaultWriteConverter();
        defaultWriteConverter.put(ConverterKeyBuild.buildKey(localDateConverter.supportJavaTypeKey()), localDateConverter);
        Map<String, Converter> allConverter=converters.loadAllConverter();
        allConverter.put(ConverterKeyBuild.buildKey(localDateConverter.supportJavaTypeKey(), localDateConverter.supportExcelTypeKey()), localDateConverter);
        return converters;
    }
}

4.2 调试代码

    /**
     * 加载全局类型转换器
     *
     * @param response
     */
    @GetMapping("/loadGlobalConverter")
    public void loadGlobalConverter(HttpServletResponse response) throws IOException {
        List<List<Object>> sheetDataList = new ArrayList<>();
        List<Object> rowList = new ArrayList<>();
        rowList.add(LocalDate.parse("2021-07-30", DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        rowList.add(new BigDecimal(0.11));
        sheetDataList.add(rowList);
        response.addHeader("Content-Disposition", "filename=test.xlsx");
        //设置类型,扩展名为.xls
        response.setContentType("application/vnd.ms-excel");
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
        WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
        excelWriter.write(sheetDataList, writeSheet);
        //千万别忘记finish 会帮忙关闭流
        excelWriter.finish();
    }

4.3 调试结果

 

注:

LocalDateConverter源码请查看以下博客。

EasyExcel 自定义类型转换器Converter

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值