尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

【源码】JeecgBoot导出Excel模板

【源码】JeecgBoot导出Excel模板
📅 发布时间:2026/7/29 18:50:54

【源码】JeecgBoot导出Excel模板

  • 一、前言
    • 1、Excel模板原型
  • 二、实现代码
    • 1、导入模板
    • 2、yml文件配置
    • 3、接口方法
    • 4、前端调取方法
  • 三、说明

版权声明:文为博主原创文章,未经博主允许不得转载。原创不易,希望大家尊重原创!
Copyright © 2026 DarLing丶张皇 保留所有权利

一、前言

JeecgBoot中Autopoi是其自带的实现导入导出功能的poi,可以方便我们更简单的实现Excel模板导出,Excel导入,Word模板导出。

使用前准备:在Maven中引入Autopoi依赖

<dependency><groupId>org.jeecgframework</groupId><artifactId>autopoi-web</artifactId><version>1.3.6</version></dependency>

所有表达式

表达式描述/示例
三目运算{{test ? obj:obj2}}test如果某一个字段,则该字段一定是布尔类型,如果一个表达式,如arg == 1则一定要带有空格
n:这个cell是数值类型 {{n:}}
le:代表长度{{le:()}} 在if/else 运用{{le:() > 8 ? obj1 : obj2}}
fd:格式化时间 {{fd:(obj;yyyy-MM-dd)}}
fn:格式化数字 {{fn:(obj;###.00)}}
fe:遍历数据,创建row
!fe遍历数据不创建row
$fe:下移插入,把当前行,下面的行全部下移.size()行,然后插入
#fe:横向遍历
v_fe:横向遍历值
!if:删除当前列 {{!if:(test)}}
单引号表示常量值 ‘’ 比如’1’ 那么输出的就是 1
&NULL&空格
]]换行符 多行遍历导出

1、Excel模板原型

上图中:t.name等为模板占位符,预先设置导出模板,通过表达式取值

二、实现代码

1、导入模板

先将模板Word导入到jeecg-boot-module-system>src>main>resources>templates下

2、yml文件配置

统一在.yml文件中配置文档模板目录,方便维护。
2.1、开发环境下,在application-dev.yml中配置:

path :
template-attendance-record-file: jeecg-boot-module-system/src/main/resources/templates/attendance_record.xlsx

2.2、生产环境下,在application-prod.yml中配置:

path :
template-attendance-record-file: jeecg-boot-module-system/src/main/resources/templates/attendance_record.xlsx

3、接口方法

Controller

// 引入模板文件存放地址@Value(value="${jeecg.path.template-attendance-record-file}")privateStringtemplateAttendanceRecordFile;/** * 导出考勤记录表Excel(模板) * templateAttendanceRecordFile为模板存放路径,请在yml文件中配置 * @param startTime * @param endTime */@GetMapping(value="/export-excel-file")publicResponseEntity<Resource>exportExcel(@RequestParam(name="startTime",required=true)StringstartTime,@RequestParam(name="endTime",required=true)StringendTime){Resourceresource=null;FiletempFile=null;try{// step.1 获取数据,并写入Excel,调用Service层导出方法org.apache.poi.ss.usermodel.Workbookworkbook=tbClockRecordService.exportExcel(startTime,entTime,templateAttendanceRecordFile);// step.2 创建临时文件保存导出结果tempFile=File.createTempFile("exported_survey_report_sample_test_file",".xlsx");FileOutputStreamfos=newFileOutputStream(tempFile);workbook.write(fos);fos.close();workbook.close();// step.3 创建FileSystemResource对象resource=newFileSystemResource(tempFile);// step.4 返回ResponseEntity,包含文件的响应returnResponseEntity.ok().header("Content-Disposition","attachment; filename=attendance_record.xlsx").body(resource);}catch(Exceptione){e.printStackTrace();log.error("考勤信息导出失败"+e.getMessage());thrownewValidationException("考勤信息失败:"+e.getMessage());}finally{// 清理临时文件(可选,取决于是否需要保留文件)if(tempFile!=null&&tempFile.exists()){tempFile.deleteOnExit();}}}

Service

/** * 导出考勤信息Excel(模板) * @param startTime 开始时间 * @param endTime 结束时间 * @param templatePath 模板地址路径 * @return */WorkbookexportExcel(StringstartTime,StringendTime,StringtemplatePath);

5、ServiceImpl

/** * 导出考勤记录Excel(模板) * @param startTime 开始时间 * @param entTime 结束时间 * @param templatePath 模板地址路径 * @return */@OverridepublicWorkbookexportExcel(StringstartTime,StringentTime,StringtemplatePath){Workbookworkbook=null;try{// Step.1、判断模板文件是否存在FiletemplateFile=newFile(templatePath);if(!templateFile.exists()){thrownewIOException("模板文件不存在:"+templatePath);}// Step.2、数据查询(仅为示例)List<TbClockRecord>tbClockRecordList=tbClockRecordService.selectList(newLambdaQueryWrapper<AreaFarm>().eq(TbClockRecord::getCreateTime,startTime).eq(TbClockRecord::getCreateTime,entTime));if(ObjectUtil.isNull(tbLeave)){thrownewJeecgBootException("未找到数据!");}// Step.3、封装模板数据(以下仅为示例,具体数据请根据自身业务赋值)List<Map<String,Object>>totalMapList=newArrayList<>();List<Map<String,Object>>listMap=newArrayList<>();// 序号,从1自增intseqNum=1;for(TbClockRecordtbClockRecord:tbClockRecordList){Map<String,Object>map=newHashMap<String,Object>();// 序号map.put("xh",seqNum);// 姓名map.put("name",tbClockRecord.getName());// 部门map.put("dept",tbClockRecord.getDept());// 上班打卡map.put("clockin",tbClockRecord.getClockin());// 下班打卡map.put("clockout",tbClockRecord.getClockout());// 打卡说明map.put("detail",tbClockRecord.getDetail());// 备注map.put("remark",tbClockRecord.getRemark());seqNum++;listMap.add(map);}totalMapList.put("maplist",listMap)// Step.4、使用Jeecg-Boot模板导出ExcelTemplateExportParamsparams=newTemplateExportParams(templatePath);workbook=ExcelExportUtil.exportExcel(params,totalMapList);}catch(Exceptione){e.printStackTrace();}returnworkbook;}

4、前端调取方法

4.1、统一的API请求管理

/** * 下载文件 用于导出 * @param url 请求地址 * @param parameter 请求参数 * @returns {*} */exportfunctiondownFile(url,parameter){returnaxios({url:url,params:parameter,method:'get',responseType:'blob'})}//get请求、post请求等此处省略

4.2、请求

// 引入API请求import{downFile}from'@/api';/** * 下载文件 用于Excel导出 * @param record 导出数据的行信息 * @returns {*} */handleExportWord(record){const{id,name}=record;letparams={id};downFile("/export-excel-file",params).then((data)=>{if(!data||data.size===0){this.$message.warning('考勤记录表下载失败!');return;}if(typeofwindow.navigator.msSaveBlob!=='undefined'){// IE/Edge 浏览器window.navigator.msSaveBlob(newBlob([data],{type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}),fileName+'.xlsx');}else{// 现代浏览器leturl=window.URL.createObjectURL(newBlob([data],{type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}));letlink=document.createElement('a');link.style.display='none';link.href=url;link.setAttribute('download',fileName+'-考勤记录表.xlsx');// 统一使用 fileNamedocument.body.appendChild(link);link.click();document.body.removeChild(link);window.URL.revokeObjectURL(url);}}).finally(()=>{this.confirmLoading=false;})})}

三、说明

预先设置导出模板,通过表达式取值,实现一些特殊样式/风格的导出,避免编写大量复杂的代码,降低开发难度,提高维护效率
word模板和Excel模板用法基本一致,支持的标签也是一致的,若需要Word模版导出,请移步前往【源码】JeecgBoot导出Word模板学习。
Excel模板导出:前往JeecgBoot文档中心参考。

原创不易,喝水莫忘挖井人,谢谢!!!

相关新闻

  • 校园运维系统轻量化改造:基于零代码搭建校园报修管理平台落地实践
  • 文生图Prompt怎么写:把模糊创意拆成七个可控要素
  • 3个步骤让你的华硕笔记本告别卡顿:G-Helper轻量级控制工具实战指南

最新新闻

  • AI开题报告工具测评与选择参考 - 逢君学术-AI论文写作
  • 【企业级提示词Ops体系】:覆盖需求对齐→AB测试→归因分析→知识沉淀的全链路闭环(含GitHub开源评估仪表盘)
  • AntiDupl.NET:告别重复图片烦恼,智能清理您的数字相册
  • 粉尘车间快速门防尘密封改造与配件更换技巧
  • VS Code Office Viewer:为什么开发者需要一站式办公文件预览解决方案?
  • 5分钟上手Pokio:PHP开发者必学的异步编程技巧

日新闻

  • 金融舆情监测系统:多语言情感分析与实时可视化技术解析
  • QT C++调用Python异常处理:PyBind11实战与跨语言编程指南
  • A-47双麦回音消除模块:主次麦空间分布与差分连接对ENC性能的影响

周新闻

  • 大连理工大学与东京大学联手打造的“主动型AI助手“
  • 170.2026年国家级科研瓶颈:超精密单点金刚石切削(SPDT)光学表面生成
  • SongBloom:革命性歌曲生成框架深度解析——如何通过交织自回归与扩散模型创作完整音乐

月新闻

  • 2026年6月公司网站搭建最新热门渠道测评:四大低成本/零代码平台对比+避坑
  • 【Linux】Linux arm 编译QT程序,出现expected “}“报错
  • 【MATLAB例程】四基站二维AOA定位与距离辅助增强对比仿真。基于角度观测和测距修正的固定目标平面定位精度分析

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号