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

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

pom.xml 引入

		<!--easypoi导入导出-->
		<dependency>
              <groupId>cn.afterturn</groupId>
              <artifactId>easypoi-spring-boot-starter</artifactId>
              <version>4.3.0</version>
         </dependency>

1、创建部门实体

注:@Data 注解用是到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、导出结果
在这里插入图片描述

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值