使用easypoi模板方法导出excel

该文章介绍了如何使用Java的Easypoi库进行Excel模板导出,包括横向和纵向输出数据,以及添加数据验证如单元格下拉框的功能。通过引入Easypoi的相关依赖,创建模板,设置参数,可以方便地生成和操作Excel文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

系列文章目录

一、Java使用Apache POI导出excel
二、Apache POI 操作Excel常用方法
三、Apache poi 拆分单元格并赋值
四、使用easypoi模板方法导出excel
五、Apache poi给excel单元格添加下拉框或数据验证
六、Apache poi操作Word常用方法



效果图

在这里插入图片描述

一、横向输出

1、创建模板

在这里插入图片描述

2、引入依赖

<dependency>
     <groupId>cn.afterturn</groupId>
     <artifactId>easypoi-spring-boot-starter</artifactId>
     <version>4.3.0</version>
</dependency>
<!-- 建议只用start -->
<dependency>
	<groupId>cn.afterturn</groupId>
	<artifactId>easypoi-base</artifactId>
	<version>4.3.0</version>
</dependency>
<dependency>
	<groupId>cn.afterturn</groupId>
	<artifactId>easypoi-web</artifactId>
	<version>4.3.0</version>
</dependency>
<dependency>
	<groupId>cn.afterturn</groupId>
	<artifactId>easypoi-annotation</artifactId>
	<version>4.3.0</version>
</dependency>

3、代码

public static void main(String[] args) {
    String s = exportExcel();
    System.out.println(s);
}

public static String exportExcel () {
    // 文件输出路径
    String excelPath = "D:\\桌面\\";

    // 模板导出
    Map<String, Object> map = new HashMap<String, Object>();
    ArrayList<Object> list = new ArrayList<>();
    for (int i = 1; i < 5; i++) {
        HashMap<String, String> lm = new HashMap<>();
        lm.put("name", "姓名" + i);
        lm.put("class", "班级" + i);
        lm.put("age", "年龄" + i);
        list.add(lm);
    }
    map.put("list", list);

    TemplateExportParams params = new TemplateExportParams(
            "./xlsx/test.xlsx");  // 模板路径
    Workbook workbook = ExcelExportUtil.exportExcel(params, map);
    File savefile = new File(excelPath);
    if (!savefile.exists()) {
        savefile.mkdirs();
    }
    String path = excelPath + "测试.xlsx";	// 文件输出路径
    try {
        FileOutputStream fos = new FileOutputStream(path);
        workbook.write(fos);
        fos.close();
        return path;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

二、纵向输出

效果图

在这里插入图片描述

1、创建模板

在这里插入图片描述

2、引入依赖

<dependency>
     <groupId>cn.afterturn</groupId>
     <artifactId>easypoi-spring-boot-starter</artifactId>
     <version>4.3.0</version>
</dependency>
<!-- 建议只用start -->
<dependency>
	<groupId>cn.afterturn</groupId>
	<artifactId>easypoi-base</artifactId>
	<version>4.3.0</version>
</dependency>
<dependency>
	<groupId>cn.afterturn</groupId>
	<artifactId>easypoi-web</artifactId>
	<version>4.3.0</version>
</dependency>
<dependency>
	<groupId>cn.afterturn</groupId>
	<artifactId>easypoi-annotation</artifactId>
	<version>4.3.0</version>
</dependency>

3、代码

public static void main(String[] args) {
    String s = exportExcel();
    System.out.println(s);
}

public static String exportExcel () {
    // 文件输出路径
    String excelPath = "D:\\桌面\\";

    // 模板导出
    Map<String, Object> map = new HashMap<String, Object>();
    ArrayList<Object> list = new ArrayList<>();
    for (int i = 1; i < 5; i++) {
        HashMap<String, String> lm = new HashMap<>();
        lm.put("name", "姓名" + i);
        lm.put("class", "班级" + i);
        lm.put("age", "年龄" + i);
        list.add(lm);
    }
    map.put("list", list);

    TemplateExportParams params = new TemplateExportParams(
            "./xlsx/test.xlsx");  // 模板路径
    params.setColForEach(true);
    Workbook workbook = ExcelExportUtil.exportExcel(params, map);
    File savefile = new File(excelPath);
    if (!savefile.exists()) {
        savefile.mkdirs();
    }
    String path = excelPath + "测试.xlsx";	// 文件输出路径
    try {
        FileOutputStream fos = new FileOutputStream(path);
        workbook.write(fos);
        fos.close();
        return path;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李子木、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值