easyexcel模板填充列表

引入依赖

xml 复制代码
   <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.6</version>
        </dependency>

编写模板

编写代码

java 复制代码
public class FillData {

    private String name;
    private Double number;

    public FillData(String name, Double number) {
        this.name = name;
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getNumber() {
        return number;
    }

    public void setNumber(Double number) {
        this.number = number;
    }
}

注意:列表填充一定要用实体对象,不能用map

java 复制代码
    @RequestMapping("/test")
    public void test() {
   
        // 模板注意 用{} 来表示你要用的变量,{.}表示列表 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
        String templateFileName = "D:\\tools\\code\\excel\\demofillsimple.xlsx";


    
        String fileName = "D:\\tools\\code\\excel\\simpleFill" + System.currentTimeMillis() + ".xlsx";
        
        List<FillData> list = new ArrayList<>();

        FillData s1 = new FillData("张三", 20.0);
        FillData s2 = new FillData("李四", 50.0);
        list.add(s1);list.add(s2);
        FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();
        ExcelWriter writer = EasyExcel.write(fileName).withTemplate(templateFileName).build();
        WriteSheet sheet1 = EasyExcel.writerSheet().sheetName("1").build();
        // 这里 会填充到第一个sheet页为名称为 1, 
        writer.fill(list,fillConfig,sheet1);
        WriteSheet sheet2 = EasyExcel.writerSheet().sheetName("2").build();
        // 这里 会填充到第一个sheet页为名称为 2, 
        writer.fill(list,fillConfig, sheet2);
        writer.finish();

    }

验证


相关推荐
?re?ta?rd?ed?16 小时前
C++11新特性,{}与左值
c++·windows
必须得开心呀18 小时前
windows10系统任务栏图标消失修复
windows
zl_dfq19 小时前
Windows 下 UDP 套接字编程指南
网络·windows·udp
学习论之费曼学习法20 小时前
无人值守AI内容矩阵实战(10):Windows 上让 Python 进程 7×24 活着
人工智能·windows·python·矩阵·powershell·进程守护
少年的范儿20 小时前
Windows 安装 MySQL 8.4 教程(ZIP 解压版):配置 my.ini、初始化数据库与启动服务
数据库·windows·mysql
胖纸不争21 小时前
EnergyStarZ:让 Windows 后台进程给前台让路的能效管理器(v1.2 发布)
windows·性能优化·c#·win32·ef core
2501_915909061 天前
Windows 上用 VS Code 调试 iPhone 真机 App,KXBuild 断点、崩溃定位实操
ide·windows·vscode·ios·iphone·个人开发·敏捷流程
情 九1 天前
MCP 开发环境搭建:Mac 与 Windows 跑通 Python 服务和 stdio 客户端
windows·python·macos·mcp
懂点Tech1 天前
ctfshow misc28:GIF 高度修改解题与脚本实现
开发语言·windows·python
云梦谭1 天前
让 Linux 主动连接 Windows:配置 Windows 11 SSH 服务端
linux·windows·ssh