easyexcel poi 一个模板导出excel包含多个sheet

easy poi 一个模板导出excel包含多个sheet

1.简述

通过调用ExcelExportUtil.exportExcelClone(map, params)实现

2.导出代码实例

@ResponseExcel
@ApiOperation(value = "导出excel表格", notes = "导出车辆信息统计excel表格")
@GetMapping("/export")
public void exportInfo(HttpServletResponse response) {
	try {
		// 获取模板文件
		String path = "excelFile/excel模板.xlsx";
		TemplateExportParams params = new TemplateExportParams(path);

		Map<Integer, List<Map<String, Object>>> map=new HashMap<>();

		//第一个sheet
		List<Map<String, Object>> mList=new ArrayList<>();
		Map<String,Object> m1=new HashMap<>();
		m1.put("title","好好学习,天天向上");
		m1.put("sheetName","sheet名称1");
		m1.put("list", VehicleVo.testData1());
		mList.add(m1);

		//第二个sheet
		List<Map<String, Object>> mList1=new ArrayList<>();
		Map<String,Object> m2=new HashMap<>();
		m2.put("title","量变达到质变");
		m2.put("sheetName","sheet名称2");
		m2.put("list", VehicleVo.testData2());
		mList1.add(m2);

		map.put(0,mList);
		map.put(1,mList1);

		Workbook workbook = ExcelExportUtil.exportExcelClone(map, params);

		// 设置文件名
		response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("年度车辆信息.xlsx", "UTF-8"));
		// 写出文件
		OutputStream os = new BufferedOutputStream(response.getOutputStream());
		// 输出
		workbook.write(os);
		os.flush();
		os.close();
		os.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class VehicleVo {

	//序号
	private Integer index;
	//驾驶员
	private String driver;
	//车牌号
	private String number;
	//车辆型号
	private String models;

	public static List<VehicleVo> testData1(){
		List<VehicleVo> list=new ArrayList<>();
		list.add(new VehicleVo(1,"张三","车牌号1","车辆型号1"));
		list.add(new VehicleVo(2,"李四","车牌号2","车辆型号2"));
		return list;
	}

	public static List<VehicleVo> testData2(){
		List<VehicleVo> list=new ArrayList<>();
		list.add(new VehicleVo(1,"张飞","车牌号33","车辆型号33"));
		list.add(new VehicleVo(2,"关羽","车牌号22","车辆型号22"));
		return list;
	}
}

3.导出模板

模板

4.导出效果

在这里插入图片描述
在这里插入图片描述

5.项目实战案例

@ResponseExcel
@ApiOperation(value = "导出excel表格", notes = "导出明细excel表格")
@GetMapping("/export/{checkDeptIds}")
public void exportInfo(@PathVariable("checkDeptIds") String checkDeptIds, HttpServletResponse response) {
	Map<Integer, List<Map<String, Object>>> maps=new HashMap<>();
	String[] ids = checkDeptIds.split(",");
	for (int i = 0; i < ids.length; i++) {
		long checkDeptId = Long.parseLong(ids[i]);
		SecrecyCheckDepartment dept = secrecyCheckDepartmentService.getById(checkDeptId);
		List<Map<String, Object>> mapList=new ArrayList<>();
		Map<String, Object> map = new HashMap<>();

		List<SecrecyCheckContent> list = secrecyCheckApproveService.getInfoById(checkDeptId);
		list.add(secrecyCheckContentService.selectHj(checkDeptId));

		map.put("year",dept.getCheckYear());
		map.put("sheetName",dept.getDeptName());
		map.put("list", list);
		mapList.add(map);
		maps.put(i,mapList);
	}

	try {
		// 获取模板文件
		String path = "excelFile/明细.xlsx";
		TemplateExportParams params = new TemplateExportParams(path);
		Workbook workbook = ExcelExportUtil.exportExcelClone(maps,params);//<!-- POI的版本要对应,不然依赖冲突,找不到方法什么的都是版本问题,版本一致的,可以忽略-->

		for (int i = 0; i < ids.length; i++) {
			Sheet sheet = workbook.getSheetAt(i);
			List<SecrecyCheckContent> list = (List<SecrecyCheckContent>) maps.get(i).get(0).get("list");
			/**合并行*/
			distributeUserService.rowspan(list, sheet,2,0,"targetName");
		}

		// 设置文件名
		response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("明细.xlsx", "UTF-8"));
		// 写出文件
		OutputStream os = new BufferedOutputStream(response.getOutputStream());
		// 输出
		workbook.write(os);
		os.flush();
		os.close();
		os.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
}
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值