EasyExcel 通过模板填充数据两种方式:1、直接通过模板填充。2、通过IO流填充。

模板示例

注意:单个字段填充只写字段名即可,数据集填充需要在字段前加 .

{title}
姓名 昵称 手机号
{.username} {.nickname} {.phone}

代码示例:

package com.service.single.example.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.service.single.entity.SystemUser; import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* @Author: 一休
* @Date: 2025/2/10
*/
public class ExcelTemplate { public static void main(String[] args) throws IOException {
// 准备数据
List<SystemUser> users = new ArrayList<>();
SystemUser user1 = new SystemUser();
user1.setUsername("张三");
user1.setNickname("zhangsan");
user1.setPhone("11111");
users.add(user1);
SystemUser user2 = new SystemUser();
user2.setUsername("张三");
user2.setNickname("zhangsan");
user2.setPhone("11111");
users.add(user2);
// 单个字段填充,使用map格式
Map<String, Object> otherData = new HashMap<>();
otherData.put("title", "系统用户表");
// 模板文件路径
String templateFileName = "src/main/resources/template.xlsx";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
String formattedDateTime = LocalDateTime.now().format(formatter);
// 输出文件路径
String outputFileName = "src/main/resources/filled_template_" + formattedDateTime + ".xlsx";
// 输出文件路径
String outputFileName1 = "src/main/resources/filled_template1_" + formattedDateTime + ".xlsx"; // 1、直接通过文件模板填充
try (ExcelWriter excelWriter = EasyExcel.write(outputFileName).withTemplate(templateFileName).build();) {
// 创建写入工作表
WriteSheet writeSheet = EasyExcel.writerSheet().build();
// 每次使用 list 参数时创建新行。
FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();
// 针对某个字段填充
excelWriter.fill(otherData, fillConfig, writeSheet);
// 填充集合
excelWriter.fill(users, fillConfig, writeSheet);
} // 2、通过IO流填充
try (
// 使用 try-with-resources 管理输入流
InputStream inputStream = Files.newInputStream(Paths.get(templateFileName));
InputStream templateInputStream = new BufferedInputStream(inputStream);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
) {
try (
// 创建 ExcelWriter 对象
ExcelWriter excelWriter = EasyExcelFactory.write(outputStream).excelType(ExcelTypeEnum.XLSX).withTemplate(templateInputStream).build()
) {
WriteSheet writeSheet = EasyExcelFactory.writerSheet().build();
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
// 针对某个字段填充
excelWriter.fill(otherData, fillConfig, writeSheet);
// 填充集合
excelWriter.fill(users, fillConfig, writeSheet);
} try (
FileOutputStream fileOutputStream = new FileOutputStream(outputFileName1)
) {
outputStream.writeTo(fileOutputStream);
// 刷新 FileOutputStream 缓冲区,确保数据全部写入文件
fileOutputStream.flush();
System.out.println("文件生成成功: " + outputFileName1);
}
}
}
}

EasyExcel 通过模板填充数据的更多相关文章

  1. 用NPOI从DataTable到Excel,向Excel模板填充数据

    DataTable---->Excel,填充数据 private IWorkbook workbook = null; private ISheet sheet = null; private ...

  2. Freemarker取list集合中数据(将模板填充数据后写到客户端HTML)

    1.模板写法: <html> <head> <title>freemarker测试</title> </head> <body> ...

  3. 根据EXCEL模板填充数据

    string OutFileName = typeName+"重点源达标率" + DateTime.Now.ToString("yyyy-MM-dd");    ...

  4. 读取excel模板填充数据 并合并相同文本单元格

    try             { string OutFileName = "北京市国控企业污染源废气在线比对监测数据审核表" + DateTime.Now.ToString(& ...

  5. POI3.10读取Excel模板填充数据后生成新的Excel文件

    private final DecimalFormat df = new DecimalFormat("#0.00"); public void test(){ String fi ...

  6. 尝试做一个.NET模板填充导出Excel工具

    园友好,最近晚辈延续上篇后尝试进阶做成Excel模板填充数据生成工具 MiniExcel Template. 主要特点 同样以Stream流.延迟查询避免全部数据载入内存情况,做到1GB内存降低到只需 ...

  7. 借助 SIMD 数据布局模板和数据预处理提高 SIMD 在动画中的使用效率

    原文链接 简介 为发挥 SIMD1 的最大作用,除了对其进行矢量化处理2外,我们还需作出其他努力.可以尝试为循环添加 #pragma omp simd3,查看编译器是否成功进行矢量化,如果性能有所提升 ...

  8. Aspose.Cells 首次使用,用到模版填充数据,合并单元格,换行

    Aspose.Cells 首次使用,用到模版填充数据,合并单元格,换行 模版格式,图格式是最简单的格式,但实际效果不是这种,实际效果图如图2 图2 ,注意看红色部分,一对一是正常的,但是有一对多的订单 ...

  9. Net 自定义Excel模板导出数据

    转载自:http://www.cnblogs.com/jbps/p/3549671.html?utm_source=tuicool&utm_medium=referral 1 using Sy ...

  10. python-Word模板填充-docxtpl

    docxtpl 按指定的word模板填充内容 安装 pip install docxtpl 示例 from docxtpl import DocxTemplate data_dic = { 't1': ...

随机推荐

  1. Clickhouse之集群操作

    查看集群: 在任意一台机上,使用 /usr/bin/clickhouse-client --host localhost --port 9000 连接本地服务器 select * from `syst ...

  2. 开发Git分支管理

    目前分支管理 AngularJS在github上的提交记录被业内大多数开发人员认可,逐渐被广泛引用. 代码提交Message格式 type (scope): message 参数介绍: 1.type: ...

  3. AI工具推荐——Cherry Studio

    Cherry Studio介绍 Cherry Studio是一款支持多模型服务的 Windows/macOS GPT 客户端. 它的主要特点如下: 多样化的大型语言模型提供商支持 ️ 主要的大型语言模 ...

  4. Arrays Basics

    `#include ; using namespace std; int main() { int A[5];//数组的声明 int B[5] = { 2,4,6,8,10 };//数组的声明和初始化 ...

  5. 腾讯云TKE-PV使用COS存储案例:容器目录权限问题

    背景 在TKE的集群中创建工作负载并把某一个对应的cos桶的根目录挂载到/data目录,在镜像构建的时候有把/data目录设置权限为755,但是运行容器后成功挂载cos桶的根目录到/data/目录,发 ...

  6. 如何在BASH中将制表符分隔值(TSV)文件转换为逗号分隔值(CSV)文件?(How do I convert a tab-separated values (TSV) file to a comma-separated values (CSV) file in BASH?)

    我有一些TSV文件需要转换为CSV文件. BASH中是否有任何解决方案,例如使用awk来转换这些?我可以这样使用sed,但担心它会出错: sed 's/\t/,/g' file.tsv > fi ...

  7. Qt开发经验小技巧121-130

    QLineEdit除了单纯的文本框以外,还可以做很多特殊的处理用途. 限制输入只能输入IP地址. 限制输入范围,强烈推荐使用 QRegExpValidator 正则表达式来处理. //正在表达式限制输 ...

  8. vue3 封装axios

    1添加一个新的 http.js文件 封装axios 引入axios //引入Axios import axios from 'axios' 定义一个根地址 //视你自己的接口地址而定 var root ...

  9. 对CGAL5.0及以后版本编译的说明

    CGAL5.0及以后版本只有头文件,没有库文件了.这意味着CGAL无需编译,只需安装好CGAL的依赖项即可.类似Eigen库.

  10. TotalUninstaller(Setup.ForcedUninstall.exe)可执行程序和源码的下载

    TotalUninstaller(Setup.ForcedUninstall.exe)可执行程序和源码的下载: 链接:https://pan.baidu.com/s/1uBiJ6z1RNVmBEUiF ...