最近都是在写导出,那么为什么要用 easypoi呢,我用freemarker模板导出写好后,发现图片不行,word转Base64编码就行,excel不行,只能换一种了,一个导出弄一天。做个记录,你知道的越多,不知道的越多。

EasyPoi官网

展示效果(正面 sheet)

(反面 sheet)

1. EasyPoi模板导出

1.1 准备模板

  • 用{{}}包裹变量就行,注意一点,图片所在的单元格不用提前合并。

  • 这边有个遍历填充的

    第一个单元格:{{$fe: maplist t.familyName
    中间单元格直接 t.变量
    最后一个单元格 t.变量 }}虽然官网说的 {{$fe: maplist t t.familyName}},但是不要这么写,不然第一列元素不填充。(t是默认)
    

1.2 准备依赖

        <dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>4.1.2</version><scope>compile</scope></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>4.1.2</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>4.1.2</version></dependency>
<!-- 正常来说上面的三个就行,版本推荐和我一样的 -->
<!-- POI的版本要对应,不然依赖冲突,找不到方法什么的都是版本问题,版本一致的,可以忽略--><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>20.0</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>4.1.2</version></dependency>

1.3 具体实现

  • 大概就是首先查出相关数据,并根据要求处理好格式,拼数据什么的,将查出来的数据以map的key-value格式进行保存,再利用ExcelExportUtil生成Excel,到系统Excel文件的固定保存位置,然后再去读这个文件,以流的形式返回给前台,windows.open(),实现导出下载。
  • 一些路径我写死的,后续改成自己yml文件里的配置就行,不建议写死。
    @GetMapping("/exportDisciplineLeader")@ApiOperation("纪检干部导出")@LogAnnotation("纪检干部导出")public Result exportDisciplineLeader(HttpServletResponse response,String oid) {try {// 查询导出数据 HashMap<String,Object> map = disciplineLeaderInfoService.listDisciplineLeader(oid);// 获取resource目录下的模板位置String templateFileName = this.getClass().getResource("/").getPath() + "excelTemplate" + File.separator + "disciplineLeader.xls";// 第二个参数true是为了开启多sheet扫描,就是同一个xls文件中有多个工作表的时候。TemplateExportParams params = new TemplateExportParams(templateFileName, true);// 数据载入,生成excel文件Workbook book = ExcelExportUtil.exportExcel(params, map);ArrayList<Map<String, String>> maplist = (ArrayList<Map<String, String>>) map.get("maplist");// 纵向合并单元格CellRangeAddress cellAddresses = new CellRangeAddress(1, maplist.size()+1, 0, 0);book.getSheet("反面").addMergedRegion(cellAddresses);try {//设置导出文件名、创建输出流FileOutputStream fos = new FileOutputStream("D:\\easypoi\\华侨城集团有限公司纪委书记(纪检干部)信息采集表.xls");//导出excel文件book.write(fos);fos.close();} catch (IOException e) {e.printStackTrace();}// 以流的形式返回给前台response.setContentType("application/force-download");// 设置强制下载不打开response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("华侨城集团有限公司纪委书记(纪检干部)信息采集表.xls", "UTF-8"));// 设置文件名byte[] buffer = new byte[1024];FileInputStream fis = null;BufferedInputStream bis = null;try {fis = new FileInputStream("D:\\easypoi\\华侨城集团有限公司纪委书记(纪检干部)信息采集表.xls");bis = new BufferedInputStream(fis);OutputStream os = response.getOutputStream();int i = bis.read(buffer);while (i != -1) {os.write(buffer, 0, i);i = bis.read(buffer);}} catch (Exception e) {e.printStackTrace();} finally {if (bis != null) {try {bis.close();} catch (IOException e) {e.printStackTrace();}}if (fis != null) {try {fis.close();} catch (IOException e) {e.printStackTrace();}}}return null;} catch (Exception e) {log.error("纪检干部导出失败", e);return Result.error().message("纪检干部导出失败");}}
  • 大多数都是拼接数据的,看图片导出的设置直接到最后。
 @Overridepublic HashMap<String, Object> listDisciplineLeader(String oid) throws IOException {HashMap<String, Object> map = new HashMap<>();DisciplineLeaderInfo disciplineLeaderInfo = disciplineLeaderInfoMapper.selectById(oid);if (disciplineLeaderInfo != null) {// 日期格式 yyyy年MM月dd日SimpleDateFormat ymdFormat = new SimpleDateFormat("yyyy年MM月dd日");SimpleDateFormat ymFormat = new SimpleDateFormat("yyyy年MM月");// 姓名map.put("personName", disciplineLeaderInfo.getPersonName());// 性别map.put("sexName", disciplineLeaderInfo.getSexName());// 民族map.put("nationName", disciplineLeaderInfo.getNationName());// 出生日期Date birthday = disciplineLeaderInfo.getBirthday();String birthdayformat = ymdFormat.format(birthday);map.put("birthday", birthdayformat);// 籍贯map.put("birthplace", disciplineLeaderInfo.getBirthplace());// 政治面貌map.put("politicStatuName", disciplineLeaderInfo.getPoliticStatuName());// 入党时间Date partyTime = disciplineLeaderInfo.getPartyTime();String partFormat = ymFormat.format(partyTime);map.put("partyTime", partFormat);// 参加工作时间Date workTime = disciplineLeaderInfo.getWorkTime();String workTimeformat = ymFormat.format(workTime);map.put("workTime", workTimeformat);// 毕业时间String[] graduationDate = disciplineLeaderInfo.getGraduationDate().split("-");map.put("graduationDate", graduationDate[0] + "年" + graduationDate[1] + "月" + graduationDate[2] + "日");// 户籍所在地map.put("censusRegisterProvinceName", disciplineLeaderInfo.getCensusRegisterProvinceName());map.put("censusRegisterCityName", disciplineLeaderInfo.getCensusRegisterCityName());map.put("censusRegisterCountyName", disciplineLeaderInfo.getCensusRegisterCountyName());// 身体状况map.put("physicalCondition", disciplineLeaderInfo.getPhysicalCondition());// 现任职务map.put("presentOccupation", disciplineLeaderInfo.getPresentOccupation());// 任现职时间String[] lengthTenureDate = disciplineLeaderInfo.getLengthTenureDate().split("-");map.put("lengthTenureDate", lengthTenureDate[0] + "年" + lengthTenureDate[1] + "月" + lengthTenureDate[2] + "日");// 学历学位-全日制map.put("allEducationBackgroundFirstName", disciplineLeaderInfo.getAllEducationBackgroundFirstName());map.put("allEducationBackgroundSecondName", disciplineLeaderInfo.getAllEducationBackgroundSecondName());// 毕业院校及专业map.put("allEducationGraduateInstitutions", disciplineLeaderInfo.getAllEducationGraduateInstitutions());// 学历学位-在职map.put("inEducationBackgroundFirstName", disciplineLeaderInfo.getInEducationBackgroundFirstName());map.put("inEducationBackgroundSecondName", disciplineLeaderInfo.getInEducationBackgroundSecondName());// 毕业院校及专业map.put("inEducationGraduateInstitutions", disciplineLeaderInfo.getInEducationGraduateInstitutions());// 专业技术职称map.put("professionalTechnical", disciplineLeaderInfo.getProfessionalTechnical());// 熟悉专业有何专长map.put("specialSkill", disciplineLeaderInfo.getSpecialSkill());// 证件号码map.put("cardId", disciplineLeaderInfo.getCardId());// 婚姻状况map.put("maritalStatusName", disciplineLeaderInfo.getMaritalStatusName());// 通讯地址map.put("mailingAddress", disciplineLeaderInfo.getMailingAddress());// 联系方式map.put("accusedTelOne", disciplineLeaderInfo.getAccusedTelOne());// 工作履历List<LzdaRecordInformationInfo> lzdaRecordInformationInfos = lzdaRecordInformationInfoMapper.selectList(Wrappers.<LzdaRecordInformationInfo>lambdaQuery().eq(LzdaRecordInformationInfo::getBusinessId, oid));if (CollectionUtils.isNotEmpty(lzdaRecordInformationInfos)) {StringBuffer workResume = new StringBuffer();for (int i = 0; i < lzdaRecordInformationInfos.size(); i++) {// 工作开始时间String[] workStartDateSplit = lzdaRecordInformationInfos.get(i).getWorkStartDate().split("-");String workStartDate = workStartDateSplit[0] + "年" + workStartDateSplit[1] + "月" + workStartDateSplit[2] + "日";// 工作结束时间String[] workEndDateSplit = lzdaRecordInformationInfos.get(i).getWorkEndDate().split("-");String workEndDate = workEndDateSplit[0] + "年" + workEndDateSplit[1] + "月" + workEndDateSplit[2] + "日";// 单位String recordworkUnit = lzdaRecordInformationInfos.get(i).getRecordworkUnit();// 职务String positionName = lzdaRecordInformationInfos.get(i).getPositionName();// 拼接数据+换行workResume.append(i + 1).append("、").append(workStartDate + "-" + workEndDate + " " + recordworkUnit + positionName).append("\n");}map.put("workResume", workResume);}// 家庭成员及主要社会关系List<LzdaFamilyRelationInfo> lzdaFamilyRelationInfos = lzdaFamilyRelationInfoMapper.selectList(Wrappers.<LzdaFamilyRelationInfo>lambdaQuery().eq(LzdaFamilyRelationInfo::getBusinessId, oid));if (CollectionUtils.isNotEmpty(lzdaFamilyRelationInfos)) {ArrayList<Map<String, String>> listMap = new ArrayList<>();for (LzdaFamilyRelationInfo lzdaFamilyRelationInfo : lzdaFamilyRelationInfos) {HashMap<String, String> famap = new HashMap<>();// 家庭成员姓名famap.put("familyName", lzdaFamilyRelationInfo.getFamilyName());// 与本人关系famap.put("familyRelationName", lzdaFamilyRelationInfo.getFamilyRelationName());// 工作单位或从事事业famap.put("familyWorkUnit", lzdaFamilyRelationInfo.getFamilyWorkUnit());// 职务famap.put("familyPositionName", lzdaFamilyRelationInfo.getFamilyPositionName());listMap.add(famap);}// 不到6行添加空数据if (lzdaFamilyRelationInfos.size() < 5) {for (int i = 0; i < 6 - lzdaFamilyRelationInfos.size(); i++) {HashMap<String, String> temp = new HashMap<>();// 家庭成员姓名temp.put("familyName", "");// 与本人关系temp.put("familyRelationName", "");// 工作单位或从事事业temp.put("familyWorkUnit", "");// 职务temp.put("familyPositionName", "");listMap.add(temp);}}map.put("maplist", listMap);}// 执纪经历List<JjgbRulesSituation> jjgbRulesSituations = jjgbRulesSituationMapper.selectList(Wrappers.<JjgbRulesSituation>lambdaQuery().eq(JjgbRulesSituation::getBusinessId, oid));if (CollectionUtils.isNotEmpty(jjgbRulesSituations)) {StringBuffer rulesSituation = new StringBuffer();for (int i = 0; i < jjgbRulesSituations.size(); i++) {// 执纪开始时间String ruleStartDate = ymFormat.format(jjgbRulesSituations.get(i).getRuleStartDate());// 执纪结束时间String ruleEndDate = ymFormat.format(jjgbRulesSituations.get(i).getRuleEndDate());// 执纪经历String ruleExperience = jjgbRulesSituations.get(i).getRuleExperience();// 拼接数据+换行rulesSituation.append(i + 1).append("、").append(ruleStartDate + "-" + ruleEndDate + " " + ruleExperience).append("\n");map.put("rulesSituation", rulesSituation);}}// 参加培训学习情况map.put("learningCondition", disciplineLeaderInfo.getLearningCondition());// 奖励处分集合List<LzdaRewardPunishmentInfo> rewardPunishmentInfoList = disciplineLeaderInfo.getRewardPunishmentInfoList();if (CollectionUtils.isNotEmpty(rewardPunishmentInfoList)) {StringBuffer rewardPunishment = new StringBuffer();for (int i = 0; i < rewardPunishmentInfoList.size(); i++) {// 拼接数据+换行rewardPunishment.append(i + 1).append("、").append(rewardPunishmentInfoList.get(i).getRewardPunishmentDesignation() + "," + rewardPunishmentInfoList.get(i).getRewardPunishmentTypeName()).append("\n");}map.put("rewardPunishment", rewardPunishment);}// 考核结果List<JjgbThreeYearAssessmentResult> threeYearAssessmentResults = disciplineLeaderInfo.getThreeYearAssessmentResults();if (CollectionUtils.isNotEmpty(threeYearAssessmentResults)) {StringBuffer assessment = new StringBuffer();for (int i = 0; i < threeYearAssessmentResults.size(); i++) {String assessmentDate = ymFormat.format(threeYearAssessmentResults.get(i).getAssessmentDate());String assessGradeName = threeYearAssessmentResults.get(i).getAssessGradeName();// 拼接数据+换行assessment.append(i + 1).append("、").append(assessmentDate + "年度考核等次为" + assessGradeName).append("\n");}map.put("assessment", assessment);}MultiMediaInfo multiMediaInfo = multiMediaInfoMapper.selectOne(Wrappers.<MultiMediaInfo>lambdaQuery().eq(MultiMediaInfo::getBusinessOid, oid));// 图片ImageEntity image = new ImageEntity();// 这里是设置合并单元格,但是千万不要再模板你提前合并单元格。合并了这里会报错。行合并多少个格子在这里设置。image.setRowspan(4);//向下合并三行//添加图片存放路径 D:\IMG_0783.JPGimage.setUrl(multiMediaInfo.getFilePath());map.put("image", image);}return map;}

EasyPoi 模板导出Excel (带图片) 以及一些踩坑记录相关推荐

  1. java导出excel带图片_JAVA的poi实现模版导出excel(带图片).doc

    JAVA的poi实现模版导出excel(带图片) 下面是本人使用java的poi实现使用模板到处excel,内容包含图片,使用两种不同的方式实现其到处excel.但是使用jxl实现到处excel只能到 ...

  2. EasyExcel根据自定义模板导出Excel(包含图片、表格)

    使用EasyExcel根据模板导出excel,包含图片.表格 提示:其实使用EasyExcel根据模板导出一个excel并不难,难点在于指定图片的位置 文章目录 使用EasyExcel根据模板导出ex ...

  3. Freemarker 模板导出(带图片)

    1. 依赖 <!--FreeMarker--><dependency><groupId>org.freemarker</groupId><arti ...

  4. poi使用模板导出word带图片

    1.下面是我做的加载模板导出带图片的word的导出方式,使用之前请映入freemarker的jar包,可以从百度下载即可. 2.首先准备到你要导出的word模板,在要填充的模板中填入el表达式,如下图 ...

  5. easypoi模板导出excel以及遇到的合并问题

    文章目录 前言 一.模板导出的关键词 二.模板填写 三.导出模板合并问题 四 结尾部分 总结 前言 背景是甲方要导出周报,但是导出的周报的样式比较复杂,只能选用模板导出,但是第一次使用模板导出遇到了一 ...

  6. asp.net 导出excel带图片

    protected void btgua_Click(object sender, EventArgs e) { DataTable dt = ds.Tables[0]; if (dt != null ...

  7. POI 导出Excel 带图片导出 使用XSSFWorkbook

    参考链接:POI导出图片到Excel不生效 (2007以上版本)_rj_han的博客-CSDN博客 //数据源 查询库 List<FormMt> mtList = formMtMapper ...

  8. nopi 导出excel 带图片

    HSSFWorkbook hssfworkbook = new HSSFWorkbook(); var sheet1 = hssfworkbook.CreateSheet("第一个Sheet ...

  9. react-quill 图片上传及图片粘贴功能踩坑记录

    Gitlab React-quill:https://github.com/zenoamaro/react-quill 中文文档 Quill:http://doc.quilljs.cn/1409381 ...

  10. springboot + 若依 ruoyi + easypoi excel的导入导出(带图片)

    springboot + 若依 ruoyi + easypoi excel的导入导出(带图片) 一.官方文档 gitee地址 官方文档 二.快速开始 1.导入 引入依赖 <dependency& ...

最新文章

  1. 为了方便读者检索和阅读以往的内容,已开通“号内搜”功能
  2. 彻底理解Python中的yield
  3. Hybris Commerce Cloud backoffice的一些使用截图 - OAuth工作中心
  4. 在Safari里也能像Chrome里一样,通过执行js修改变量的值,在debugger里立即生效
  5. Maven基础:Maven环境搭建及基本使用(1)
  6. java设计模式迭代器模式_迭代器模式和Java
  7. linux修改java内存大小_Linux 和 windows修改java虚拟机内存大小
  8. XML学习笔记--导航
  9. 应用chroot构建最小运行系统
  10. 简单Git入门本地仓库同步到远程GitHub仓库
  11. Linux 虚拟机内挂载 iso 文件
  12. 提交代码到gitbub.com
  13. zxing 二维码生成深度定制
  14. NLP-阅读理解-2015:MRC模型-指导机器去阅读并理解【开篇之作】【完形填空任务】【第一次构建大批量有监督机器阅读理解训练语料】【三种模型结构:LSTM、Attention、Impatient】
  15. Ubuntu安装百度云盘
  16. [499]openstack swift 的UI客户端
  17. DevOps工具链 - 2021
  18. 新学期,在这里写下我人生的新规划
  19. #STC8A8K# #STC8F1K# #STC8G1K# #STC8H1K# ——STC8系列单片机整体评价
  20. bootstrap3- 导航条 - 慕课笔记

热门文章

  1. 计算机材料学常用计算软件,计算机在材料科学中的应用-用MaterialsStudio计算简单材料的能带.doc...
  2. 牛客 彩虹 【经典状压dp】
  3. Matlab魔方矩阵的创建及逻辑矩阵的用法
  4. 怎么使用svn下载到本地
  5. 推荐一款华为最新的自动化代码检查工具
  6. 安科瑞ACY100油烟浓度在线监控仪在浙江省某市餐饮油烟监测治理项目中的应用
  7. Androidstudio svn 使用 版本控制 详细步骤(一)
  8. TensorRt - caffe中支持prelu
  9. 浏览器实现word在线预览
  10. 实现一个圆形进度条(vue)