当前位置: 首页 > article >正文

EasyExcel 多个不同对象集合,导入同一个sheet中

需求:将两块数据,写入要一个excel里面。

根据项目,导入依赖版本 

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

1、创建对象,用于不同数据接收


import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@Schema(description = "")
public class ExecuteVo {

	private static final long serialVersionUID = 1L;

	@Schema(description = "")
	@ExcelProperty("")
	private String indexName;

	@Schema(description = "")
	@ExcelProperty("")
	private String name1;

	@Schema(description = "")
	@ExcelProperty("")
	private String name2;

}
import com.alibaba.excel.annotation.ExcelProperty;
import com.ewx.starinet.archsys.entity.SysBusinessTreenodes;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serializable;
@Data
@Schema(description = "")
public class YearCheckedVo {

	private static final long serialVersionUID = 1L;

	@Schema(description = "")
	@ExcelProperty("")
	private String indexName;

	@Schema(description = "")
	@ExcelProperty("")
	private String name1;

	@Schema(description = "")
	@ExcelProperty("")
	private String name2;

	@Schema(description = "")
	@ExcelProperty("")
	private String name3;

	@Schema(description = "")
	@ExcelProperty("")
	private String name4;

	@Schema(description = "")
	@ExcelProperty("")
	private String name5;
}

2、具体实现

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class Tests {
    public static void main(String[] args) throws IOException {
        // 文件保存地址
        String path = ;
        File file = new File(path);
        // 不存在目录就创建目录
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }

        File file1 = new File(path + "s.xls");
        if(!file1.exists()){
            file1.createNewFile();
        }

        List<ExecuteVo> list1 = 实体1;

        List<YearCheckedVo> list2 = 实体2;

        ExcelWriter excelWriter = null;
        try {
            excelWriter = EasyExcel.write(file1).build();
            // 把sheet设置为不需要头 不然会输出sheet的头 这样看起来第一个table 就有2个头了
            WriteSheet writeSheet = EasyExcel.writerSheet().needHead(Boolean.FALSE).build();
            // 这里必须指定需要头,table 会继承sheet的配置,sheet配置了不需要,table 默认也是不需要
            WriteTable writeTable1 = EasyExcel.writerTable(0).head(DemoVo1.class).needHead(Boolean.TRUE).build();
            // 第二个对象 读取对象的excel实体类中的标题
            WriteTable writeTable2 = EasyExcel.writerTable(1).head(DemoVo2.class).needHead(Boolean.TRUE).build();
            // 第一次写入会创建头
            excelWriter.write(list1, writeSheet, writeTable1);
            // 第二次写如也会创建头,然后在第一次的后面写入数据
            excelWriter.write(list2, writeSheet, writeTable2);

        }finally {
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }
}

需要注意的是这里

excelWriter.finish();

结束的时候文件里面才会出现内容


http://www.kler.cn/news/323360.html

相关文章:

  • gMLP:Pay Attention to MLPs--模型代码讲解
  • 数字通云平台智慧政务 login 存在登录绕过
  • Java | Leetcode Java题解之第435题无重叠区间
  • E9OA解决文档附件没有关联文档正文问题
  • 54K55LyB5p2l5a6i5pyN57O757uf token硬编码漏洞
  • Spring源码学习:SpringMVC(2)DispatcherServlet初始化【子容器9大组件】
  • 对于 Vue CLI 项目如何引入Echarts以及动态获取数据
  • 机器学习-SVM
  • xxl-job 适配达梦数据库
  • StarRocks Elasticsearch Catalog原理简析
  • 【机器学习】目标分类算法概述
  • UI设计师面试整理-作品集展示
  • 基于Hive和Hadoop的招聘分析系统
  • GUI-窗口,模态窗口,拖动窗口
  • centos离线安装nvm
  • 2024新版IDEA创建JSP项目
  • 查看和升级pytorch到指定版本
  • 如何让 Android 的前端页面像 iOS 一样“优雅”?
  • 从 ES5 到 ES14:深入解析 JavaScript 的演进与特性
  • 828华为云征文|部署去中心化网络的 AI 照片管理应用 PhotoPrism
  • 【教程】最新可用! 移动云手机开启Root权限方法
  • 探索甘肃非遗:Spring Boot网站开发案例
  • Ansible-触发器_打标签
  • winsoft公司Utils组件功能简介
  • 27 基于51单片机的方向盘模拟系统
  • 入选ECCV 2024!覆盖5.4w+图像,MIT提出医学图像分割通用模型ScribblePrompt,性能优于SAM
  • vue2 将页面生成pdf下载
  • 【深度学习】05-RNN循环神经网络-03- batch/epoch在神经网络中指的是什么
  • 2024 CSP 游记
  • mysql索引 -- 聚簇索引,非聚簇索引,如何查看linux下的数据库文件,普通/辅助索引(回表查询)