easy poi 导出功能(表格一对多)

最近在导出功能,查到easypoi 做个简单的工具类,希望帮助到您,自己做下收藏!

pom.xml 文件

    <dependency>
	        <groupId>cn.afterturn</groupId>
	        <artifactId>easypoi-base</artifactId>
	        <version>3.2.0</version>
	</dependency>
	<dependency>
	        <groupId>cn.afterturn</groupId>
	        <artifactId>easypoi-web</artifactId>
	        <version>3.2.0</version>
	</dependency>
	<dependency>
	       <groupId>cn.afterturn</groupId>
	       <artifactId>easypoi-annotation</artifactId>
	       <version>3.2.0</version>
	</dependency>

1、创建部门实体

注:@Datee 注解用是到lombok插件

@Data
@ExcelTarget("empUtil")	
public class DeptUtil {@Excel(name = "部门编号", width = 30 , needMerge = true)
private Integer id;

@Excel(name = "部门名称", width = 30 , needMerge = true)
private String deptName;

@ExcelCollection(name = "员工信息")
private List<EmpUtil> emps;

}

2、创建员工实体

@Data
@ExcelTarget("empUtil")
public class EmpUtil {

@Excel(name = "序号", width = 30, isColumnHidden = true)
private Integer id;

@Excel(name = "员工姓名", width = 30, groupName = "基本信息")
private String empName;

@Excel(name = "年龄", width = 30, type = 10, groupName = "基本信息")
private Integer age;

@Excel(name = "入职时间", width = 30, groupName = "工作信息", format = "yyyy/MM/dd HH:mm")
private Date hiredate;

@Excel(name = "薪酬", width = 30, type = 10, groupName = "工作信息")
private BigDecimal salary;

}

3、demo方法

注:demo中写的一些测试数据,根据自身业务的情况进行简单封装。

@GetMapping("export")
public void export(QueryRequest queryRequest, FilingRecord filingRecord, HttpServletResponse response)
		throws FebsException {
	try {
		// 设置响应输出的头类型
		response.setHeader("content-Type", "application/vnd.ms-excel");
		// 下载文件的默认名称
		response.setHeader("Content-Disposition", "attachment;filename=user.xls");
		// =========easypoi部分
		List<DeptUtil> exportList = new LinkedList<DeptUtil>();
		DeptUtil deptUtil = new DeptUtil();
		deptUtil.setId(1);
		deptUtil.setDeptName("研发部");
		
		List<EmpUtil> ll = new LinkedList<EmpUtil>();
		EmpUtil empUtil = new EmpUtil();
		empUtil.setId(1);
		empUtil.setAge(20);
		empUtil.setEmpName("研发");
		empUtil.setHiredate(new Date());
		
		EmpUtil empUtil2 = new EmpUtil();
		empUtil2.setId(1);
		empUtil2.setAge(20);
		empUtil2.setEmpName("测试");
		empUtil2.setHiredate(new Date());
		ll.add(empUtil);
		ll.add(empUtil2);
		deptUtil.setEmps(ll);
		exportList.add(deptUtil);
		ExportParams deptExportParams = new ExportParams();
		// 设置sheet得名称
		deptExportParams.setSheetName("员工报表1");
		
		Map<String, Object> deptExportMap = new HashMap<>();
		// title的参数为ExportParams类型,目前仅仅在ExportParams中设置了sheetName
		deptExportMap.put("title", deptExportParams);
		// 模版导出对应得实体类型
		deptExportMap.put("entity", DeptUtil.class);
		// sheet中要填充得数据
		deptExportMap.put("data", exportList);
		
		List<Map<String, Object>> sheetsList = new ArrayList<>();
		sheetsList.add(deptExportMap);
		Workbook workbook =ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
		ServletOutputStream outputStream = response.getOutputStream();
		workbook.write(outputStream);
		outputStream.flush();
		outputStream.close();
	} catch (Exception e) {
		log.error(e.getMessage(), e);
	}
}

4、导出结果
在这里插入图片描述
5、如果员工实体下再有个List
在员工实体类中注解增加
@ExcelCollection(name = “角色信息”)
private List roles;

原文链接:https://blog.csdn.net/qq_31984879/article/details/102715335

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用easy-poi导出word模板中的表格时,需要按照以下步骤进行操作。 首先,我们需要准备一个word模板文件,其中包含了我们想要导出表格的样式和布局。可以使用Microsoft Word或其他支持word格式的编辑软件创建和编辑模板。 接下来,我们需要使用easy-poi的API来读取和处理模板文件。首先,我们需要创建一个`TemplateExportParams`对象,指定模板文件的路径。 然后,我们可以通过调用`ExcelExportUtil.exportWord`方法来根据模板生成word文件。在导出过程中,我们可以使用`Map`或`List<Map>`对象作为数据源,用于填充模板中的表格单元格。 对于简单的表格,我们可以使用`Map`对象来存储数据。其中,键对应模板中的字段名,值对应字段要显示的数据。如果我们需要填充多行表格,可以使用`List<Map>`来存储多个`Map`对象。 在代码中,我们可以使用以下语句来导出word文件: ```java String templatePath = "模板文件路径"; String outputPath = "导出文件保存路径"; TemplateExportParams exportParams = new TemplateExportParams(templatePath); Map<String, Object> map = new HashMap<>(); map.put("表格数据", 数据源); Workbook workbook = ExcelExportUtil.exportWord(exportParams, map); FileOutputStream fos = new FileOutputStream(outputPath); workbook.write(fos); fos.close(); ``` 其中,"表格数据"是模板中指定的字段名,数据源是存储表格数据的`Map`或`List<Map>`对象。 最后,我们可以保存生成的word文件到指定的输出路径。通过调用`workbook.write`方法将`Workbook`对象写入到输出流中,即可保存为word文件。 以上就是使用easy-poi导出word模板表格的主要步骤。通过简单配置模板和填充数据,我们可以轻松地生成符合需要的word文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值