jeecgBoot基于easyPoi非注解方式下载动态表头模板及数据

jeecgBoot基于easyPoi非注解方式下载动态表头模板及数据

应用场景描述

可实现非固定实体下模板导出,不用依赖于注解,可根据自己需要实现进行表头拼接

模板效果

在这里插图片描述表头是根据需要进行拼接组装的

后端代码

	public void exportTempXls(HttpServletResponse response) {
		try {
			//=========================== 生成表头 start ===========================
			List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
			//构造对象等同于@Excel
			ExcelExportEntity excelentity = new ExcelExportEntity("班级名称", "className");
			entity.add(excelentity);
				excelentity = new ExcelExportEntity("姓名", "name");
			entity.add(excelentity);
				excelentity = new ExcelExportEntity("学生成绩", "achievement");
				List<ExcelExportEntity> temp = new ArrayList<ExcelExportEntity>();
				temp.add(new ExcelExportEntity("语文","chiness"));
				temp.add(new ExcelExportEntity("数学", "mathematics"));
				temp.add(new ExcelExportEntity("英语", "english"));
				excelentity.setList(temp);
			entity.add(excelentity);
				excelentity = new ExcelExportEntity("特长", "specialty");
			entity.add(excelentity);
				excelentity = new ExcelExportEntity("家庭信息", "family");
				temp = new ArrayList<ExcelExportEntity>();
				temp.add(new ExcelExportEntity("电话","tel"));
				temp.add(new ExcelExportEntity("住址", "address"));
				excelentity.setList(temp);
			entity.add(excelentity);
			//=========================== 生成表头 end ===========================

			//=========================== 构建数据结构 start ========================
			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			List<Map<String,Object>> dataList=new ArrayList<>();
			//数据表辅助信息
			Map<String, Object> map = new HashMap<>();
			Map<String, Object> dataMap = new HashMap<>();
			map.put("className","一年二班");
			map.put("name","小明");
				dataMap.put("chiness","99");
				dataMap.put("mathematics","99");
				dataMap.put("english","99");
				dataList.add(dataMap);
			map.put("achievement",dataList);
			map.put("specialty","运动、看书");
				dataList=new ArrayList<>();
				dataMap = new HashMap<>();
				dataMap.put("tel","15978709909");
				dataMap.put("address","人民广场");
				dataList.add(dataMap);
			map.put("family",dataList);
			list.add(map);
			//=========================== 构建数据结构 end ===========================


			String fileName="示例模板";
			Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(fileName, fileName), entity,list);
			response.setContentType("application/vnd.ms-excel");
			response.addHeader("Content-Disposition", "attachment;filename=tastName");
			BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
			workbook.write(bos);
			bos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

前端代码

vue代码

  <a @click="handleExportXls()" >模板下载</a>
 methods: {
      handleExportXls(){
        let   fileName =record.name+"示例模板"
        downFile(this.url.exportXlsUrl,{}).then((data)=>{
          if (!data) {
            this.$message.warning("文件下载失败")
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), fileName+'.xls')
          }else{
            let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', fileName+'.xls')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link); //下载完成移除元素
            window.URL.revokeObjectURL(url); //释放掉blob对象
          }
        })
      },
    }

第一次写如有错误请多多指教

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值