第一步导入依赖:

<!--excel支持-->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.11</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>3.11</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.11</version>
</dependency>

第二步前端控制器实现:

/*** 日志下载* @param year* @param month* @param daily* @param name* @param depts* @param response* @return*/@GetMapping("download")public ResponseEntity download(Integer year, Integer month, Integer daily, String name, String depts, HttpServletResponse response) throws ParseException {if(StringUtils.isEmpty(year)||StringUtils.isEmpty(month)){return new ResponseEntity(SupConstant.BAD_PARAMETER, HttpStatus.BAD_REQUEST);}SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");//起始时间Date stateTime;//结束时间Date endTime;if(StringUtils.isEmpty(daily)){stateTime=format.parse(year+"-"+month+"-01");if(month==12){endTime=format.parse((year+1)+"-01-01");}else {endTime=format.parse(year+"-"+(month+1)+"-01");}}else {stateTime=format.parse(year+"-"+month+"-"+daily);endTime=new Date(stateTime.getTime()+24*60*60*1000);}Wrapper<DailyRecord> wrapper=new EntityWrapper<>();wrapper.ge("record_time",stateTime);wrapper.lt("record_time",endTime);//用户名if (!StringUtils.isEmpty(name)) {wrapper.like("creator_name",name);}//部门集合if(!StringUtils.isEmpty(depts)){wrapper.andNew();String[] split = depts.split("-");for (int i = 0; i < split.length; i++) {String s = split[i];if(i==0){wrapper.where("dept_id={0}",s);}else {wrapper.or("dept_id={0}",s);}}}List<DailyRecord> dailyRecords = dailyRecordService.selectList(wrapper);if (dailyRecords.size()>0) {//创建excel生成对象SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();Sheet sheet = sxssfWorkbook.createSheet("journail");//创建标题行Row row = sheet.createRow(0);//创建样式对象CellStyle cellStyle = sxssfWorkbook.createCellStyle();//设置背景cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);//创建字体样式Font font = sxssfWorkbook.createFont();font.setFontHeightInPoints((short) 14);//设置字体大小font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//字体加粗//赋予字体样式cellStyle.setFont(font);//标题数组String[] titles={"姓名","部门","岗位","日志应填日期","实际填写日期","领导指示安排","批注建议","个人工作日志","批注建议","工作完成情况","批注建议","问题及思路","批注建议"};for (int i = 0; i < titles.length; i++) {Cell cell = row.createCell(i);cell.setCellValue(titles[i]);cell.setCellStyle(cellStyle);}for (int i = 0; i < dailyRecords.size(); i++) {DailyRecord dailyRecord = dailyRecords.get(i);//内容Row row1 = sheet.createRow(i + 1);row1.createCell(0).setCellValue(dailyRecord.getCreatorName());row1.createCell(1).setCellValue(dailyRecord.getDeptName());if(!StringUtils.isEmpty(dailyRecord.getCreatorPostion())){row1.createCell(2).setCellValue(dailyRecord.getCreatorPostion());}row1.createCell(3).setCellValue(dailyRecord.getRecordTime());row1.createCell(4).setCellValue(dailyRecord.getGmtCreate());if(!StringUtils.isEmpty(dailyRecord.getArrange())){row1.createCell(5).setCellValue(dailyRecord.getArrange());}if(!StringUtils.isEmpty(dailyRecord.getCreatorPostion())){row1.createCell(6).setCellValue(dailyRecord.getArrangeComment());}if(!StringUtils.isEmpty(dailyRecord.getPlan())){row1.createCell(7).setCellValue(dailyRecord.getPlan());}if(!StringUtils.isEmpty(dailyRecord.getPlanComment())){row1.createCell(8).setCellValue(dailyRecord.getPlanComment());}if(!StringUtils.isEmpty(dailyRecord.getFinish())){row1.createCell(9).setCellValue(dailyRecord.getFinish());}if(!StringUtils.isEmpty(dailyRecord.getFinishComment())){row1.createCell(10).setCellValue(dailyRecord.getFinishComment());}if(!StringUtils.isEmpty(dailyRecord.getAdvise())){row1.createCell(11).setCellValue(dailyRecord.getAdvise());}if(!StringUtils.isEmpty(dailyRecord.getAdviseComment())){row1.createCell(12).setCellValue(dailyRecord.getAdviseComment());}}//创建文件名String filename = "日志.xlsx";//创建输出对象OutputStream out;response.setContentType("application/ms-excel;charset=UTF-8");try {response.setHeader("Content-Disposition", "attachment;filename=" +new String(filename.getBytes("gb2312"), "ISO8859-1"));out = response.getOutputStream();sxssfWorkbook.write(out);// 将数据写出去log.info("导出" + filename + "成功!");out.close();return new ResponseEntity("数据导出成功", HttpStatus.OK);} catch (Exception e) {e.printStackTrace();log.info("导出" + filename + "失败!");return new ResponseEntity("数据导出失败", HttpStatus.OK);}}else {return new ResponseEntity(SupConstant.NOT_INQUIRED,HttpStatus.OK);}}

第三步测试页面js(条件模拟a标签):

//下载日志
function download(){var work= $('form[id="tree_form"]').serializeArray();//年var year=$("select[name='year']").val();//月var month=$("select[name='month']").val();if(year==""||month==""){createDomHint.msgIter('请选择日期')return;}let  a = document.createElement('a');if(work.length>0){if(work.length==total){a.href = apiExtend.journal.download + '?current=' + page.current + '&size=' + page.size + '&depts=&'+ $('#header_conter_left_form').serialize(),document.body.appendChild(a),a.click(),document.body.removeChild(a)}else {var ids="";for (var i = 0; i < work.length; i++) {ids += work[i].value+"-";}a.href = apiExtend.journal.download + '?current=' + page.current + '&size=' + page.size + '&depts='+ids+'&'+ $('#header_conter_left_form').serialize(),document.body.appendChild(a),a.click(),document.body.removeChild(a)}}else {a.href = apiExtend.journal.download + '?current=' + page.current + '&size=' + page.size + '&depts=&'+ $('#header_conter_left_form').serialize(),document.body.appendChild(a),a.click(),document.body.removeChild(a)}}

第四步实现效果:

根据数据批量生成excel文件相关推荐

  1. 腾讯云文字识别API提取表格数据并生成Excel文件

    腾讯云文字识别API提取表格数据并生成Excel文件 本文主要介绍了利用腾讯云表格文字识别API提取图片表格数据并生成Excel文件.主要涉及的知识点有:腾讯云API的调用.json文件的处理以及Ex ...

  2. python爬取沪深所有股票数据并生成Excel文件

    爬取沪深所有股票数据并生成Excel文件 一.分析需求 1.对于沪深两市的各只股票,获取其:'股票代码', '股票名称', '最高', '最低', '涨停', '跌停', '换手率', '振幅', ' ...

  3. 批量生成Excel文件,可以按模板进行自动生成

    目录 一.文件目录结构 二.编辑生成名单(名单.xlsx) 三.编辑模板(模板.xlsx) 四.生成操作(批量生成Excel.exe) 五.下载地址 软件描述:根据Excel模板 和 生成名单 可以批 ...

  4. vb6 数据自动生成excel文件_Excel随机生成数据

    工作中有时候需要大量数据,而手头没有现成的数据怎么办?,很多小伙伴苦思冥想编写数据,意义不大还累个半死,今天推出两种方法随机生成数据小妙招. 1.用rand函数生成A与B之间的随机数字(A≤随机数≤B ...

  5. 【文字识别】腾讯云API:提取表格数据并生成Excel文件

    一.使用工具及python包介绍 腾讯云API 国内大型互联网公司都提供云服务,如阿里.百度.腾讯等.本文选择腾讯云服务,是因为提供的API说明比较详细,看一遍就能用.更良心的是,提供了在线测试的功能 ...

  6. 如何从Excel表格导入数据批量生成二维码

    目前二维码应用渐趋广泛,二维码具有储存量大.保密性高.追踪性高.抗损性强.备援性大.成本便宜等特性,这些特性特别适用于表单.安全保密.追踪.证照.存货盘点.资料备援等方面.那么我们怎么用条码打印软件从 ...

  7. 【如何批量从PDF提取数据并生成excel】

    如何批量从PDF提取数据并生成excel 最近帮同学处理一些扫描生成的统计年鉴,需要将里面的数据提取出来到excel文件中,但是市面上能找到的要么是收费,要么是识别不准确.结果乱码等等.在GitHub ...

  8. 如何通过Excel数据批量生成DM码

    DM码指的就是最常用的Data Matrix 二维码,Data Matrix二维码原名Data code,是由美国国际资料公司于1989年发明.DM码需要选择编码容量大的二维码,而Data Matri ...

  9. 如何从Excel表格导入数据批量生成二维码 1

    目前二维码应用渐趋广泛,二维码具有储存量大.保密性高.追踪性高.抗损性强.备援性大.成本便宜等特性,这些特性特别适用于表单.安全保密.追踪.证照.存货盘点.资料备援等方面.那么我们怎么用条码打印软件从 ...

最新文章

  1. 被批伪开源!刚刚融资6千万美元的Redis怎么了?
  2. MYSQL 中的LEFT( RIGHT ) JOIN使用ON 与WHERE 筛选的差异
  3. Java生成Word文档
  4. jenkins与gitlab集成,分支提交代码后自动构建任务(六)
  5. MySQL亿级数据量实时同步,小米如何完美hold住
  6. 51单片机中使用ucos ii的优缺点(好文)
  7. Java封装图书信息类
  8. Spring事务管理的demo
  9. php7.0不出结合项,帝国CMS结合项提示“您来自的链接不存在”
  10. ordfilt2非线性滤波器
  11. java开发面试中经常问到的问题(2019年5月)
  12. mybatis报错解决
  13. 关于ie7下display:inline-block;不支持的解决方案。
  14. SpringBoot @Value 读取配置,太强大了!
  15. 如何用管理员权限打开CMD(快捷键)
  16. 设计师必备,6个PNG素材网站
  17. 彻底删除浏览器毒霸首页
  18. python爬不同图片分别保存在不同文件夹中
  19. springboot毕设项目东莞汉庭酒店的酒店管理系统的设计与实现4ccnv(java+VUE+Mybatis+Maven+Mysql)
  20. matlab冲激激励,实验一 阶跃响应与冲激响应.doc

热门文章

  1. Linux:驱动之自动创建字符设备的设备文件(未完)
  2. 电动除草机驱动方案设计开发
  3. 小鱼赚钱app是真的吗?小鱼赚钱下载试玩应用教程
  4. 盛大创新院许式伟:影响我一生的五个重要选择
  5. K - C语言实验 圆柱体计算
  6. 分支限界——TSP问题
  7. 荧光染料标记蛋白质,Cy3/Cy5.5/Cy7-BSA/HAS/Transferrin/Concanavalin A/Casein/Ovalbumin
  8. 医疗器械网电源部分使用一个保险丝还是两个保险丝?
  9. 【UML】软件需求说明书
  10. 电动车电池管理系统结构