Java实现导出Excel功能
刚写了个导入Excel表格,现在来写个导出,其实形式都差不多,废话不多说,贴代码
<div> <button type="button" class="btn-btn" @click="exportData()">导出</button> </div>
这里根据个人需求,我这里写时间设置,根据条件去请求导出,这里因人而异
exportData(){ //判断用户有无填写操作类型 if (this.type != "") { //默认全部操作类型,不传参 this.param["code"] = this.code; } //判断用户有无填写开始日期 if (this.startDate != '') { this.param['statrDate'] = new Date(this.startDate); } //判断用户有无填写结束日期 if (this.endDate != '') { this.param['endDate'] = new Date(this.endDate); } this.param[this.optionSelected] = this.param.searchText; var url = "${ctx}/operatelog/exportOperateLog"; window.open(url); }
java中Controller层,来接受请求,数据库查询到的数据进行封装,然后使用ExcelUtils进行输出。
@RequestMapping("/exportOperateLog") @ResponseBody public void exportOperateLog(HttpServletRequest request, HttpServletResponse response, OperateLogParam param) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); try { response.reset(); response.setContentType("text/html;charset=GBK");// 设置response内容的类型 response.setHeader("Content-disposition", "attachment;filename=SysteamLog" + dateFormat.format(new Date()) + ".xls;"); String strTableName = "系统日志"; //这个是标题 List<String> headers = new ArrayList<>(); headers.add("序号"); //这里的表头,根据数据的字段命名也行,随你喜欢 headers.add("操作日期时间"); headers.add("操作类型"); headers.add("数据1"); headers.add("数据2"); headers.add("操作员id"); headers.add("操作员"); List<OperateLogParam> operateLogList = operateLogService.findOperateLogList(param); List<String[]> operateListStr = new ArrayList<>(); for (int i = 0; i < operateLogList.size(); i++) { OperateLogParam opeLogPar = operateLogList.get(i); String[] strarr = new String[]{(StringUtils.isEmpty(opeLogPar.getOperateLogId().toString())?"-":opeLogPar.getOperateLogId().toString()), (StringUtils.isEmpty(opeLogPar.getCreateTime().toString())?"-":opeLogPar.getCreateTime().toString()), (StringUtils.isEmpty(opeLogPar.getTitle())?"-":opeLogPar.getTitle()), (StringUtils.isEmpty(opeLogPar.getData1())?"-":opeLogPar.getData1()), (StringUtils.isEmpty(opeLogPar.getData2())?"-":opeLogPar.getData2()), (StringUtils.isEmpty(opeLogPar.getAdminId().toString())?"-":opeLogPar.getAdminId().toString()), (StringUtils.isEmpty(opeLogPar.getOperatorName())?"-":opeLogPar.getOperatorName()), }; operateListStr.add(strarr); } ExcelUtil.createExcel(strTableName, headers, operateListStr, response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); }
这里里面的ExcelUtils 在我的另一篇笔记中有
Excel导入功能
https://www.jb51.net/article/121533.htm
导出效果
到此这篇关于Java实现导出Excel功能的文章就介绍到这了,更多相关java导出excel内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)