文件打包下载excel导出和word导出

0.文件下载接口

        请求 GET

        /pm/prj/menu/whsj/download/{affixId}

       文件affixId多个id以逗号隔开。多个文件会以打包得形式。

 

1.Excel导出

        1.0接口

POST

127.0.0.1:8400/pm/io/exportExcel/year-plan-table-workflow/report

参数

[{"org":"011","report":"年度计划表","fiscalYear":"2023","mofDivCode":"371000"}] 

 

    1.1配置模板

数据要以 [entity.object]为模板配置,而且下方必须空一行不然导入会报错

1.2导入模板配置导出配置

多个集合需要配置多个查询条件,配置得服务接口要跟代码里面得对应服务接口得代码

package com.wenzheng.whsj.prj.export;import com.wenzheng.module.common.excel.suite.service.DataSourceProvider;
import com.wenzheng.platform.core.bean.LoginUser;
import com.wenzheng.whsj.prj.persistence.entity.YearPlanInfo;
import com.wenzheng.whsj.prj.service.PmPrjConcentrateArgumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;
import java.util.Map;/**** 年度计划表导出** @Author ZRP* @Date 2023/10/13 14:36*/
@Service(value = "gzw.yearPlanTableCloudProjectExport")
public class YearPlanTableCloudProjectExport implements DataSourceProvider {@Autowiredprivate PmPrjConcentrateArgumentService pmPrjConcentrateArgumentService;@Overridepublic List<Map<String, Object>> findData(Map<String, Object> param) {LoginUser user = new LoginUser();user.setFiscalYear(param.get("fiscalYear").toString());user.setMofDivCode(param.get("mofDivCode").toString());user.setOrgCode(param.get("org").toString());YearPlanInfo yearPlanInfo = pmPrjConcentrateArgumentService.selectYearPlan(0, user, null, null, null, null);return yearPlanInfo.getCloudProjectList();}
}

2.导出word

     2.0接口

年度计划书导出 接口

POST

http://10.30.4.96:8400/pm/io/exportExcel/year-plan-book-workflow/report

参数

[{"org":"011","report":"年度计划书","fiscalYear":"2023","mofDivCode":"371000"}]

   2.1模板

2.2配置模板

2.3编写替换数据代码

 @Autowiredprivate PmTemplateAffixService templateAffixService;@Autowiredprotected SuiteExportService exportService;@Autowiredprivate OfficeService officeService;@Value("${base.uploadpath:upload}")private String uploadPath;@Autowiredprivate PmBaseAffixMapper affixMapper;@Autowiredprivate FundsAnalysisService fundsAnalysisService;@Overridepublic ResponseEntity<byte[]> downloadWordReport(HttpServletRequest request, String id, Map<String, Object> params, LoginUser user) throws Exception {if (BaseUtils.isNull(id)) {return null;}if (org.apache.commons.collections4.MapUtils.isEmpty(params)) {params = new HashMap<>();params.put("fiscalYear", user.getFiscalYear());params.put("mofDivCode", user.getMofDivCode());}String useObject = exportService.getExpKeyByExportSwitch("year-plan-table-workflow-report", params, user.getFiscalYear(), user.getMofDivCode(), SuiteExportService.TYPE_WORD);// 取得模板List<PmTemplateAffix> lstTemplate = templateAffixService.selectByUseObject(useObject, user.getFiscalYear(),user.getMofDivCode());if (lstTemplate == null || lstTemplate.isEmpty()) {throw new TemplateSetException("模板没定义");}PmTemplateAffix affixTemp = lstTemplate.get(0);byte[] data = affixTemp.getFileData();//查询数据Map<String, Object> map = pmPrjMeasurementReferenceDao.selectById(id);if (map == null) {throw new TemplateSetException("暂无当前数据");}map = initData(map, user, request);String prjName = map.get("prj_name").toString();String fileName = prjName + affixTemp.getFileName().substring(0, affixTemp.getFileName().indexOf(".")) + "." + affixTemp.getFileType();// 替换data = officeService.createDoc(data, map);createFile(data, map, lstTemplate.get(0), fileName);//如果是生成的,则直接返回 如果是下载用下面这些代码直接输出文件流HttpHeaders headers = new HttpHeaders();// 处理文件名编码问题String userAgent = request.getHeader("user-agent");if (HttpUtils.isMSBrowser(userAgent)) {// 如果是IE浏览器,则用URLEncode解析fileName = URLEncoder.encode(fileName, "UTF-8");fileName = fileName.replace("+", " ");} else {// 如果是谷歌、火狐则解析为ISO-8859-1fileName = new String(fileName.getBytes("gbk"), StandardCharsets.ISO_8859_1);}headers.set("Content-Disposition", "attachment; filename=\"" + fileName + "\"");headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);return new ResponseEntity<>(data, headers, HttpStatus.OK);}//创建文件出来,存入文件库 获得文件要下载文件id
public String createFile(byte[] data, Map<String, Object> map, PmTemplateAffix pmTemplateAffix, String fileName) {String useObject = "yearPlanReport";DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");String dateStr = dateFormat.format(new Date());String uuidName = UUID.randomUUID().toString();String fileRealName = UUID.randomUUID().toString();File templateAffixFile = null;try {templateAffixFile = FileTool.fileToBytes(data, uploadPath + "/" + useObject + "/" + dateStr + "/", "/" + uuidName);} catch (Exception e) {System.err.println("===========>" + e.getMessage());}MultipartFile multipartFile = FileTool.getMultipartFile(templateAffixFile);PmBaseAffix affix = new PmBaseAffix();affix.setAffixId(UUID.randomUUID().toString());if (map.get("affix_id") != null) {affix.setPrjId(map.get("affix_id").toString());} else {affix.setPrjId(UUID.randomUUID().toString());}affix.setJobId(affix.getPrjId());affix.setUseObject(useObject);affix.setFileType(pmTemplateAffix.getFileType());affix.setFileSize(BaseUtils.sizeParse(multipartFile.getSize()));affix.setFileName(fileName);affix.setFileTitle(fileName);affix.setFileRealName(uuidName);affix.setFilePath(File.separator + affix.getUseObject() + File.separator + dateStr);affix.setUpdateTime(new Date());List<PmBaseAffix> pmBaseAffix = pmPrjMeasurementReferenceDao.selectByPrjCode(affix.getPrjId(), useObject);if (pmBaseAffix.size() == 0) {affixMapper.insert(affix);} else {affix.setAffixId(pmBaseAffix.get(0).getAffixId());affixMapper.updateByPrimaryKeySelective(affix);}return affix.getAffixId();}

用到的工具方法

/*** 将Byte数组转换成文件** @param bytes    byte数组* @param filePath 文件路径  如 D:\\Users\\Downloads\\* @param fileName 文件名*/public static File fileToBytes(byte[] bytes, String filePath, String fileName) {BufferedOutputStream bos = null;FileOutputStream fos = null;File file = null;try {file = new File(filePath + fileName);if (!file.getParentFile().exists()) {//文件夹不存在 生成file.getParentFile().mkdirs();}fos = new FileOutputStream(file);bos = new BufferedOutputStream(fos);bos.write(bytes);} catch (Exception e) {e.printStackTrace();} finally {if (bos != null) {try {bos.close();} catch (IOException e) {e.printStackTrace();}}if (fos != null) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}}return file;}public static  MultipartFile getMultipartFile(File file) {FileInputStream fileInputStream = null;MultipartFile multipartFile = null;try {fileInputStream = new FileInputStream(file);multipartFile = new MockMultipartFile(file.getName(), file.getName(),ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);} catch (Exception e) {e.printStackTrace();}return multipartFile;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/145291.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JSX看着一篇足以入门

JSX 介绍 学习目标&#xff1a; 能够理解什么是 JSX&#xff0c;JSX 的底层是什么 概念&#xff1a; JSX 是 javaScriptXML(HTML) 的缩写&#xff0c;表示在 JS 代码中书写 HTML 结构 作用&#xff1a; 在 React 中创建 HTML 结构&#xff08;页面 UI 结构&#xff09; 优势&a…

HarmonyOS开发:Log工具类源码分析

前言 一转眼就十月中旬了&#xff0c;国庆的劲真大&#xff0c;到现在还未缓过来&#xff0c;以至于要更新的文章迟迟未发布&#xff0c;大家可以看到&#xff0c;最近一段时间的文章&#xff0c;都是关于HarmonyOS相关的&#xff0c;两个原因吧&#xff0c;一是我司有这样的任…

操作系统——死锁及其解决方案(p38-p41王道视频、课本ch6)

1.死锁的“知识框架”&#xff1a; 2.“预防死锁”——破坏死锁的4个必要条件: 3.避免死锁&#xff01;&#xff01;&#xff01;&#xff01;——必考&#xff1a;银行家算法 安全性算法描述&#xff1a; 4.“死锁的检测和解除”:

CVer从0入门NLP(一)———词向量与RNN模型

&#x1f34a;作者简介&#xff1a;秃头小苏&#xff0c;致力于用最通俗的语言描述问题 &#x1f34a;专栏推荐&#xff1a;深度学习网络原理与实战 &#x1f34a;近期目标&#xff1a;写好专栏的每一篇文章 &#x1f34a;支持小苏&#xff1a;点赞&#x1f44d;&#x1f3fc;、…

h5的扫一扫功能 (非微信浏览器环境下)

必须在 https 域名下才生效 <template><div><van-field label"服务商编码" right-icon"scan" placeholder"扫描二维码获取" click-right-icon"getCameras" /> <div class"scan" :style"{disp…

鸿鹄工程项目管理系统 Spring Cloud+Spring Boot+Mybatis+Vue+ElementUI+前后端分离构建工程项目管理系统项目背景

鸿鹄工程项目管理系统 Spring CloudSpring BootMybatisVueElementUI前后端分离构建工程项目管理系统 1. 项目背景 一、随着公司的快速发展&#xff0c;企业人员和经营规模不断壮大。为了提高工程管理效率、减轻劳动强度、提高信息处理速度和准确性&#xff0c;公司对内部工程管…

2-MySQL的基本操作记录

1 数据库相关 -- --------------------表相关的---------- -- 查看字符集 show variables like %character%;show databases;# 创建数据库 create database test2;# 删除数据库 drop database test2; show databases;#查看当前使用的数据库 select database(); 2 用户相关 -…

CUDA学习笔记(九)Dynamic Parallelism

本篇博文转载于https://www.cnblogs.com/1024incn/tag/CUDA/&#xff0c;仅用于学习。 Dynamic Parallelism 到目前为止&#xff0c;所有kernel都是在host端调用&#xff0c;CUDA Dynamic Parallelism允许GPU kernel在device端创建调用。Dynamic Parallelism使递归更容易实现…

小游戏外包开发流程及费用

小游戏的开发流程和费用会因项目的规模、复杂性和所选技术平台而有所不同。以下是一般的小游戏开发流程和可能的费用因素&#xff0c;希望对大家有所帮助。北京木奇移动技术有限公司&#xff0c;专业的软件外包开发公司&#xff0c;欢迎交流合作。 开发流程&#xff1a; 概念和…

分享画PAD图的软件-PADFlowChart

软件的可执行文件下载&#xff1a;PADFlowChart-exe.zip 如果有帮助望三联

【每日一题】老人的数目

文章目录 Tag题目来源题目解读解题思路方法一&#xff1a;遍历 其他语言python3 写在最后 Tag 【遍历】【数组】【2023-10-23】 题目来源 2678. 老人的数目 题目解读 找出乘客中年龄严格大于 60 的人数。 解题思路 方法一&#xff1a;遍历 本题比较简单&#xff0c;直接遍…

2023 年 12 款最佳免费 PDF 阅读器

12 大最佳免费 PDF 阅读器 PDF 阅读器是一种可以打开 PDF 文件的软件&#xff0c;PDF 文件可能是最流行的文档格式。尽管 PDF 文件已经存在超过 25 年&#xff0c;但它仍然是 Internet 上文档的主要格式。但是&#xff0c;要打开此类文档&#xff0c;您必须在计算机上下载指定…