开源操作操作Exceleasy-excel使用学习教程下载Excel生产下载Java代码

网友投稿 396 2022-09-27

开源操作操作Exceleasy-excel使用学习教程下载Excel生产下载Java代码

后台Service层实现

public Workbook downloadExcel(List eventTagProcessVos) throws CorpusException { File resultFile = null; //1、查询返回List限制200条数据 File tempPathDir = null; XSSFWorkbook resultXSSFWorkbook = null; try { List eventTagProcesses= dozerGenerator.convert(eventTagProcessVos, EventTagProcess.class); String specialId = DateUtils.getSysDateId(); //创建临时文件夹 String tempPath = path + specialId; tempPathDir = new File(tempPath); if (!tempPathDir.isDirectory()) { tempPathDir.mkdir(); } //2、生成映射成excel String templatePath = tempPath + File.separator + specialId + ".xlsx"; OutputStream ops = new FileOutputStream(templatePath); ExcelExportResult exportResult = context.createExcelForPart(excelId, eventTagProcesses); //假设这是第二次从数据库或其他平台查询到到数据 Workbook workbook = exportResult.build(); workbook.write(ops); ops.close(); workbook.close(); //3、保存之后返回 File result[] = tempPathDir.listFiles(); for (int i = 0; i < result.length; i++) { if (result[i].getName().equals(specialId + ".xlsx")) { resultFile = result[i]; break; } } FileInputStream fis = new FileInputStream(resultFile); resultXSSFWorkbook = new XSSFWorkbook(fis); } catch (Exception ex) { if (null != tempPathDir) { tempPathDir.delete(); } throw new CorpusException("文件下载出现错误:" + ex.getMessage()); } return resultXSSFWorkbook; } @Override public Workbook downloadExcelByIds(String[] ids) throws CorpusException { File resultFile = null; //1、查询返回List限制200条数据 File tempPathDir = null; XSSFWorkbook resultXSSFWorkbook = null; try { List eventTagProcesses = eventTagProcessDao.queryEventTagProcessList(ids); String specialId = DateUtils.getSysDateId(); //创建临时文件夹 String tempPath = path + specialId; tempPathDir = new File(tempPath); if (!tempPathDir.isDirectory()) { tempPathDir.mkdir(); } //2、生成映射成excel String templatePath = tempPath + File.separator + specialId + ".xlsx"; //需求概述.数据量较大,可能大批量数据导出,会对DB造成压力,这里分批次检索数据,一部分一部分向Excel中写 OutputStream ops = new FileOutputStream(templatePath); ExcelExportResult exportResult = context.createExcelForPart(excelId, eventTagProcesses); //假设这是第二次从数据库或其他平台查询到到数据 Workbook workbook = exportResult.build(); workbook.write(ops); ops.close(); workbook.close(); //3、保存之后返回 File result[] = tempPathDir.listFiles(); for (int i = 0; i < result.length; i++) { if (result[i].getName().equals(specialId + ".xlsx")) { resultFile = result[i]; break; } } FileInputStream fis = new FileInputStream(resultFile); resultXSSFWorkbook = new XSSFWorkbook(fis); } catch (Exception ex) { if (null != tempPathDir) { tempPathDir.delete(); } throw new CorpusException("文件下载出现错误:" + ex.getMessage()); } return resultXSSFWorkbook; }

后台查询之后数据传入:

public File downloadInfoTemplate(EventTagProcessVo eventTagProcessVo) throws CorpusException { File resultFile = null; //1、查询返回List限制200条数据 File tempPathDir = null; EventTagProcess eventTagProcess = dozerGenerator.convert(eventTagProcessVo, EventTagProcess.class); XSSFWorkbook resultXSSFWorkbook = null; try { List eventTagProcesses = eventTagProcessDao.queryEventTagProcessList(eventTagProcess); String specialId = DateUtils.getSysDateId(); //创建临时文件夹 String tempPath = path + specialId; tempPathDir = new File(tempPath); if (!tempPathDir.isDirectory()) { tempPathDir.mkdir(); } //2、生成映射成excel String templatePath = tempPath + File.separator + specialId + ".xlsx"; //需求概述.数据量较大,可能大批量数据导出,会对DB造成压力,这里分批次检索数据,一部分一部分向Excel中写 OutputStream ops = new FileOutputStream(templatePath); ExcelExportResult exportResult = context.createExcelForPart(excelId, eventTagProcesses); Workbook workbook = exportResult.build(); workbook.write(ops); ops.close(); workbook.close(); //3、保存之后返回 resultFile = new File(templatePath); } catch (Exception ex) { if (null != tempPathDir) { tempPathDir.delete(); } throw new CorpusException("文件下载出现错误:" + ex.getMessage()); } return resultFile; }

excelTemplate.xml映射配置:

Controller层实现下载代码:

/***** * 条件查询之后进行导出Excel * @param * @return * @throws CorpusException */ @ApiOperation(value = "批量下载标注列表根据对象查询最多导出200条数据") @RequestMapping(value = "/downloadEventTagProcessExcel", method = RequestMethod.GET) public CorpusResponse downloadEventTagProcessExcel(@RequestBody @ApiParam(value = "条件查询进行下载") EventTagProcessQueryParam param, HttpServletResponse throws CorpusException { CorpusResponse response = CorpusResponseBuilder.newSuccessResponse(); try { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhMMss"); String date = sdf.format(new Date()); String fileName = "标注列表" + date + ".xlsx"; fileName = URLEncoder.encode(fileName, "utf-8"); "attachment;filename=" + fileName); String beginDate = param.getBeginDate(); String endDate = param.getEndDate(); Integer status = param.getStatus(); PageVo pageVo = eventTagProcessFacade.conditionalQuery(beginDate, endDate, status, param.getCurrent(), param.getSize()); Workbook workbook = eventTagProcessFacade.downloadExcel(pageVo.getRecords()); OutputStream stream = workbook.write(stream); stream.flush(); stream.close(); } catch (IOException e) { response = CorpusResponseBuilder.newErrorResponse(e); } return response; } /******* * 选择ID进行多项选择ID进行后台下载操作 * @param ids * @param * @return * @throws CorpusException */ @RequestMapping(value = "/downloadMultiEventTagProcessVo", method = RequestMethod.GET) @ApiOperation(value = "选择ID进行多项选择ID进行后台下载操作") public CorpusResponse downloadMultiEventTagProcessVo(@RequestParam @ApiParam(value = "id主键数组") String[] ids, HttpServletResponse throws CorpusException { CorpusResponse response = CorpusResponseBuilder.newSuccessResponse(); try { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhMMss"); String date = sdf.format(new Date()); String fileName = "标注列表" + date + ".xlsx"; fileName = URLEncoder.encode(fileName, "utf-8"); "attachment;filename=" + fileName); Workbook workbook = eventTagProcessFacade.downloadExcelByIds(ids); OutputStream stream = workbook.write(stream); stream.flush(); stream.close(); } catch (IOException e) { response = CorpusResponseBuilder.newErrorResponse(e); } return response; }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Zookeeper客户端的jar用bat启动cmd运行jar
下一篇:Spring的Model 和 Map的原理源码解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~