当前位置: 首页 > news >正文

JAVA生成Word文档

第一步:导入依赖

<!--生成word文档-->
<dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.10.3</version>
</dependency>
<!--数字转为汉字大写-->
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId>
</dependency>

第二步:业务代码 

  @Overridepublic void downloadEngineeringWord(HttpServletResponse response, Long engineeringId) {String engineeringWordSql = "你的查询sql";Map<String, Object> tendersWord = (Map<String, Object>) SqlUtil.getMapToList(engineeringWordSql, entityManager).get(0);if (ObjectUtils.isEmpty(tendersWord)) {return;}//判断文本是否出现if (tendersWord.containsKey("margin") && !ObjectUtils.isEmpty(tendersWord.get("margin"))) {BigDecimal price = (BigDecimal) tendersWord.get("margin");tendersWord.put("marginShow", price.signum() > 0);} else {tendersWord.put("marginShow", false);}//将数字金额转为汉字大写if (tendersWord.containsKey("maxPrice") && !ObjectUtils.isEmpty(tendersWord.get("maxPrice"))) {double money = Double.parseDouble(tendersWord.get("maxPrice").toString());String chineseMoney = NumberChineseFormatter.format(money, true, true);tendersWord.put("highestPrice", chineseMoney);}//对前端使用\n换行的段落文本进行格式化换行Object qualifications = tendersWord.get("qualifications");if (!ObjectUtils.isEmpty(qualifications) && qualifications.toString().contains("/")) {List<String> qualificationsList = Arrays.asList(qualifications.toString().split("/"));List<Map<String, Object>> qualificationMapList = new ArrayList<>();qualificationsList.stream().filter(f -> !ObjectUtils.isEmpty(f.trim())).forEach(qualification -> {Map<String, Object> itemMap = new HashMap<>();itemMap.put("qualification", qualification);qualificationMapList.add(itemMap);});tendersWord.put("qualifications", qualificationMapList);}String fileName = "工程文件";try {File wordTemplate = new File("start/src/main/resources/" + fileName + ".docx");XWPFTemplate template = XWPFTemplate.compile(wordTemplate.getAbsolutePath()).render(tendersWord);response.setCharacterEncoding("UTF-8");response.setContentType("multipart/form-data");response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".docx", "utf-8"));response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");OutputStream out = response.getOutputStream();BufferedOutputStream bos = new BufferedOutputStream(out);template.write(bos);bos.flush();out.flush();PoitlIOUtils.closeQuietlyMulti(template, bos, out);} catch (IOException e) {throw new BizException(fileName + ".docx 生成失败,请稍后再试!");}}

官网链接:Poi-tl Documentation 

http://www.lryc.cn/news/294291.html

相关文章:

  • python将.db数据库文件转成Excel文档
  • [opencvsharp]C#基于Fast算法实现角点检测
  • 群晖NAS开启FTP服务结合内网穿透实现公网远程访问本地服务
  • ReactNative实现弧形拖动条
  • STM32F407移植OpenHarmony笔记9
  • telnet笔记
  • 【考研408】操作系统笔记
  • 从0开始搭建、上传npm包
  • 【Go】在 JSON 中解析 time.Duration
  • UE4 C++ UGameInstance实例化
  • 在工业制造方面,如何更好地实现数字化转型?
  • 【MySQL】-10 MySQL 存储过程
  • 3.闭包 - JS
  • Java实现批量视频抽帧2.0
  • MFC 原生LsitCtrl单元格嵌入图标
  • 黑马头条 - minio
  • 认识Tomcat (一)
  • SSH免密切换服务器案例-ssh协议(公钥和私钥)
  • AMH面板如何安装与公网远程访问本地面板界面
  • 【教3妹学编程-算法题】1696. 跳跃游戏 VI
  • 解决C#中无限递归导致的System.StackOverflowException异常
  • ASP.NET Core 预防开放式重定向攻击
  • HashCat 恢复Excel、Word、PPT密码保姆教程
  • flink实战--flink的job_listener使用解析
  • ASR 概述
  • 聊聊比特币----比特币地址
  • (4)【Python数据分析进阶】Machine-Learning模型与算法应用-回归、分类模型汇总
  • Python 调用 OpenAI ChatGPT API
  • springboot155基于JAVA语言的在线考试与学习交流网页平台
  • echarts使用之地图(五)