easyexcel处理复杂表头

需求,模板如下

在这里插入图片描述

功能如下

在这里插入图片描述

开始整活,依赖包。

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>easyexcel</artifactId>
	<version>3.2.1</version>
</dependency>

下载导入模板


1.方法

@GetMapping("/download")
    public void download(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "xxxx导入模板", BasicIntegralDownloadVo.class, response);
    }

2.实体

@ExcelIgnoreUnannotated
@Data
public class BasicIntegralDownloadVo {

    private static final long serialVersionUID = 1L;

    @ColumnWidth(value = 8)
    @ExcelProperty(value = {"积分制积分明细表","户名"},index = 0)
    private String name;

    @ColumnWidth(value = 8)
    @ExcelProperty(value = {"积分制积分明细表","人口类型"},index = 1)
    private String populationType;

    @ColumnWidth(value = 8)
    @ExcelProperty(value = {"积分制积分明细表","门牌号"},index = 2)
    private String houseNumber;

    @ColumnWidth(value = 8)
    @ExcelProperty(value = {"积分制积分明细表","联系方式"},index = 3)
    private String mobile;

    @ColumnWidth(value = 8)
    @ExcelProperty(value = {"积分制积分明细表","身份证"},index = 4)
    private String idCard;

    @ColumnWidth(value = 8)
    @ExcelProperty(value = {"积分制积分明细表","积分年度"},index = 5)
    private String integralYear;

    @ColumnWidth(value = 10)
    @ExcelProperty(value = {"积分制积分明细表","农户零星养殖鸡鸭棚规范管理", "第1季度"},index = 6)
    private Integer type1;

    @ColumnWidth(value = 10)
    @ExcelProperty(value = {"积分制积分明细表","农户零星养殖鸡鸭棚规范管理", "第2季度"},index = 7)
    private Integer type2;

    @ColumnWidth(value = 10)
    @ExcelProperty(value = {"积分制积分明细表","农户零星养殖鸡鸭棚规范管理", "第3季度"},index = 8)
    private Integer type3;

    @ColumnWidth(value = 10)
    @ExcelProperty(value = {"积分制积分明细表","农户零星养殖鸡鸭棚规范管理", "第4季度"},index = 9)
    private Integer type4;

    @ColumnWidth(value = 10)
    @ExcelProperty(value = {"积分制积分明细表","小菜园规范种植、禁止毁绿占绿进行种植", "第1季度"},index = 10)
    private Integer type5;

    @ColumnWidth(value = 10)
    @ExcelProperty(value = {"积分制积分明细表","小菜园规范种植、禁止毁绿占绿进行种植", "第2季度"},index = 11)
    private Integer type6;

    @ColumnWidth(value = 10)
    @ExcelProperty(value = {"积分制积分明细表","小菜园规范种植、禁止毁绿占绿进行种植", "第3季度"},index = 12)
    private Integer type7;

    @ColumnWidth(value = 10)
    @ExcelProperty(value = {"积分制积分明细表","小菜园规范种植、禁止毁绿占绿进行种植", "第4季度"},index =13)
    private Integer type8;
}

导出工具类

	public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, HttpServletResponse response) {
        try {
            resetResponse(sheetName, response);
            ServletOutputStream os = response.getOutputStream();
            exportExcel(list, sheetName, clazz, false, os);
        } catch (IOException e) {
            throw new RuntimeException("导出Excel异常");
        }
    }
    
	/**
     * 重置响应体
     */
    public static void resetResponse(String sheetName, HttpServletResponse response) throws UnsupportedEncodingException {
        String filename = encodingFilename(sheetName);
        FileUtils.setAttachmentResponseHeader(response, filename);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");
    }

	/**
     * 导出excel
     *
     * @param list      导出数据集合
     * @param sheetName 工作表的名称
     * @param clazz     实体类
     * @param merge     是否合并单元格
     * @param os        输出流
     */
    public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz, boolean merge, OutputStream os) {
        ExcelWriterSheetBuilder builder = EasyExcel.write(os, clazz)
            .autoCloseStream(false)
            // 自动适配
            .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
            // 大数值自动转换 防止失真
            .registerConverter(new ExcelBigNumberConvert())
            .sheet(sheetName);
        if (merge) {
            // 合并处理器
            builder.registerWriteHandler(new CellMergeStrategy(list, true));
        }
        builder.doWrite(list);
    }

导入

	/**
     * 导入
     */
    @PostMapping("/import")
    public R<Void> importResident(MultipartFile file) throws IOException {
        ExcelResult<BasicIntegralDownloadVo> result = ExcelUtil.importExcel2(file.getInputStream(), BasicIntegralDownloadVo.class, new BasicIntegralListener());
        return R.ok(result.getAnalysis());
    }

	导入监听器
	//todo 无奈,东西有点多,不想写了噗嗤。

相关推荐

  1. easyExcel 获取多个sheet中复杂表头的数据

    2024-04-09 11:48:01       42 阅读
  2. 阿里 EasyExcel 表头国际化

    2024-04-09 11:48:01       32 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-09 11:48:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-09 11:48:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-09 11:48:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-09 11:48:01       18 阅读

热门阅读

  1. 1006 换个格式输出整数

    2024-04-09 11:48:01       13 阅读
  2. Flutter 使用flutter_swiper_null_safety 实现轮播图

    2024-04-09 11:48:01       12 阅读
  3. css不知道宽度,如何绘制一个正方形

    2024-04-09 11:48:01       14 阅读
  4. Getshell sql注入

    2024-04-09 11:48:01       12 阅读
  5. Mysql

    Mysql

    2024-04-09 11:48:01      13 阅读