• 文件打包下载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导入模板配置导出配置

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

    1. package com.wenzheng.whsj.prj.export;
    2. import com.wenzheng.module.common.excel.suite.service.DataSourceProvider;
    3. import com.wenzheng.platform.core.bean.LoginUser;
    4. import com.wenzheng.whsj.prj.persistence.entity.YearPlanInfo;
    5. import com.wenzheng.whsj.prj.service.PmPrjConcentrateArgumentService;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.stereotype.Service;
    8. import java.util.List;
    9. import java.util.Map;
    10. /**
    11. *
    12. * 年度计划表导出
    13. *
    14. * @Author ZRP
    15. * @Date 2023/10/13 14:36
    16. */
    17. @Service(value = "gzw.yearPlanTableCloudProjectExport")
    18. public class YearPlanTableCloudProjectExport implements DataSourceProvider {
    19. @Autowired
    20. private PmPrjConcentrateArgumentService pmPrjConcentrateArgumentService;
    21. @Override
    22. public List> findData(Map param) {
    23. LoginUser user = new LoginUser();
    24. user.setFiscalYear(param.get("fiscalYear").toString());
    25. user.setMofDivCode(param.get("mofDivCode").toString());
    26. user.setOrgCode(param.get("org").toString());
    27. YearPlanInfo yearPlanInfo = pmPrjConcentrateArgumentService.selectYearPlan(0, user, null, null, null, null);
    28. return yearPlanInfo.getCloudProjectList();
    29. }
    30. }

    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编写替换数据代码

    1. @Autowired
    2. private PmTemplateAffixService templateAffixService;
    3. @Autowired
    4. protected SuiteExportService exportService;
    5. @Autowired
    6. private OfficeService officeService;
    7. @Value("${base.uploadpath:upload}")
    8. private String uploadPath;
    9. @Autowired
    10. private PmBaseAffixMapper affixMapper;
    11. @Autowired
    12. private FundsAnalysisService fundsAnalysisService;
    13. @Override
    14. public ResponseEntity<byte[]> downloadWordReport(HttpServletRequest request, String id, Map params, LoginUser user) throws Exception {
    15. if (BaseUtils.isNull(id)) {
    16. return null;
    17. }
    18. if (org.apache.commons.collections4.MapUtils.isEmpty(params)) {
    19. params = new HashMap<>();
    20. params.put("fiscalYear", user.getFiscalYear());
    21. params.put("mofDivCode", user.getMofDivCode());
    22. }
    23. String useObject = exportService.getExpKeyByExportSwitch("year-plan-table-workflow-report", params, user.getFiscalYear(), user.getMofDivCode(), SuiteExportService.TYPE_WORD);
    24. // 取得模板
    25. List lstTemplate = templateAffixService.selectByUseObject(useObject, user.getFiscalYear(),
    26. user.getMofDivCode());
    27. if (lstTemplate == null || lstTemplate.isEmpty()) {
    28. throw new TemplateSetException("模板没定义");
    29. }
    30. PmTemplateAffix affixTemp = lstTemplate.get(0);
    31. byte[] data = affixTemp.getFileData();
    32. //查询数据
    33. Map map = pmPrjMeasurementReferenceDao.selectById(id);
    34. if (map == null) {
    35. throw new TemplateSetException("暂无当前数据");
    36. }
    37. map = initData(map, user, request);
    38. String prjName = map.get("prj_name").toString();
    39. String fileName = prjName + affixTemp.getFileName().substring(0, affixTemp.getFileName().indexOf(".")) + "." + affixTemp.getFileType();
    40. // 替换
    41. data = officeService.createDoc(data, map);
    42. createFile(data, map, lstTemplate.get(0), fileName);
    43. //如果是生成的,则直接返回 如果是下载用下面这些代码直接输出文件流
    44. HttpHeaders headers = new HttpHeaders();
    45. // 处理文件名编码问题
    46. String userAgent = request.getHeader("user-agent");
    47. if (HttpUtils.isMSBrowser(userAgent)) {
    48. // 如果是IE浏览器,则用URLEncode解析
    49. fileName = URLEncoder.encode(fileName, "UTF-8");
    50. fileName = fileName.replace("+", " ");
    51. } else {
    52. // 如果是谷歌、火狐则解析为ISO-8859-1
    53. fileName = new String(fileName.getBytes("gbk"), StandardCharsets.ISO_8859_1);
    54. }
    55. headers.set("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    56. headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    57. return new ResponseEntity<>(data, headers, HttpStatus.OK);
    58. }
    59. //创建文件出来,存入文件库 获得文件要下载文件id
    60. public String createFile(byte[] data, Map map, PmTemplateAffix pmTemplateAffix, String fileName) {
    61. String useObject = "yearPlanReport";
    62. DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    63. String dateStr = dateFormat.format(new Date());
    64. String uuidName = UUID.randomUUID().toString();
    65. String fileRealName = UUID.randomUUID().toString();
    66. File templateAffixFile = null;
    67. try {
    68. templateAffixFile = FileTool.fileToBytes(data, uploadPath + "/" + useObject + "/" + dateStr + "/", "/" + uuidName);
    69. } catch (Exception e) {
    70. System.err.println("===========>" + e.getMessage());
    71. }
    72. MultipartFile multipartFile = FileTool.getMultipartFile(templateAffixFile);
    73. PmBaseAffix affix = new PmBaseAffix();
    74. affix.setAffixId(UUID.randomUUID().toString());
    75. if (map.get("affix_id") != null) {
    76. affix.setPrjId(map.get("affix_id").toString());
    77. } else {
    78. affix.setPrjId(UUID.randomUUID().toString());
    79. }
    80. affix.setJobId(affix.getPrjId());
    81. affix.setUseObject(useObject);
    82. affix.setFileType(pmTemplateAffix.getFileType());
    83. affix.setFileSize(BaseUtils.sizeParse(multipartFile.getSize()));
    84. affix.setFileName(fileName);
    85. affix.setFileTitle(fileName);
    86. affix.setFileRealName(uuidName);
    87. affix.setFilePath(File.separator + affix.getUseObject() + File.separator + dateStr);
    88. affix.setUpdateTime(new Date());
    89. List pmBaseAffix = pmPrjMeasurementReferenceDao.selectByPrjCode(affix.getPrjId(), useObject);
    90. if (pmBaseAffix.size() == 0) {
    91. affixMapper.insert(affix);
    92. } else {
    93. affix.setAffixId(pmBaseAffix.get(0).getAffixId());
    94. affixMapper.updateByPrimaryKeySelective(affix);
    95. }
    96. return affix.getAffixId();
    97. }

    用到的工具方法

    1. /**
    2. * 将Byte数组转换成文件
    3. *
    4. * @param bytes byte数组
    5. * @param filePath 文件路径 如 D:\\Users\\Downloads\\
    6. * @param fileName 文件名
    7. */
    8. public static File fileToBytes(byte[] bytes, String filePath, String fileName) {
    9. BufferedOutputStream bos = null;
    10. FileOutputStream fos = null;
    11. File file = null;
    12. try {
    13. file = new File(filePath + fileName);
    14. if (!file.getParentFile().exists()) {
    15. //文件夹不存在 生成
    16. file.getParentFile().mkdirs();
    17. }
    18. fos = new FileOutputStream(file);
    19. bos = new BufferedOutputStream(fos);
    20. bos.write(bytes);
    21. } catch (Exception e) {
    22. e.printStackTrace();
    23. } finally {
    24. if (bos != null) {
    25. try {
    26. bos.close();
    27. } catch (IOException e) {
    28. e.printStackTrace();
    29. }
    30. }
    31. if (fos != null) {
    32. try {
    33. fos.close();
    34. } catch (IOException e) {
    35. e.printStackTrace();
    36. }
    37. }
    38. }
    39. return file;
    40. }
    41. public static MultipartFile getMultipartFile(File file) {
    42. FileInputStream fileInputStream = null;
    43. MultipartFile multipartFile = null;
    44. try {
    45. fileInputStream = new FileInputStream(file);
    46. multipartFile = new MockMultipartFile(file.getName(), file.getName(),
    47. ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
    48. } catch (Exception e) {
    49. e.printStackTrace();
    50. }
    51. return multipartFile;
    52. }

  • 相关阅读:
    [HDLBits] Count clock
    通关GO语言13 参数传递:值、引用及指针之间的区别?
    Linux环境下Redis 集群部署
    【CIKM 2023】扩散模型加速采样算法OLSS,大幅提升模型推理速度
    【论文阅读】Decision Transformer: Reinforcement Learning via Sequence Modeling
    java【毕业设计】项目-第117期基于SpringBoot+Shiro的通用权限管理系统-毕业设计
    python项目,关闭本地电脑,使程序运行在服务器上
    vue2(1)
    Map集合中,当添加一个键值对元素时,HashMap发生了什么?
    线性代数|机器学习-P23梯度下降
  • 原文地址:https://blog.csdn.net/qq_38092788/article/details/133899621