word模板导出word文件

前期准备工作word模板
右键字段如果无编辑域 ctrl+F9 一下,然后再右键
在这里插入图片描述

wps 直接 ctrl+F9 会变成编辑域
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

pom.xml所需依赖

<dependencies>
<!--word 依赖-->
<dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.core</artifactId><version>2.0.2</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.document</artifactId><version>2.0.2</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.template</artifactId><version>2.0.2</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.document.docx</artifactId><version>2.0.2</version></dependency><dependency><groupId>fr.opensagres.xdocreport</groupId><artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId><version>2.0.2</version></dependency></dependencies><plugins><!--模板是放入项目中,编辑的时候会破坏模板结构,导致模板文件类型不支持pom文件增加文件过滤(maven ckean后重试)
--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>2.6</version><configuration><encoding>UTF-8</encoding><nonFilteredFileExtensions><nonFilteredFileExtension>docx</nonFilteredFileExtension></nonFilteredFileExtensions></configuration></plugin></plugins>

Controller 层

@Slf4j
@RestController
@RequestMapping("/word")
public class WordController{@Autowiredprivate WordService  word;/*** * @param response * @param query 查询参数*/@GetMapping("/export")public void export(HttpServletResponse response,  CommonQuery query){word.export(response,query);}
}

Service 层

public interface WordService  {void export(response,query);
}
package com.Lichao.word;import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
import lombok.extern.slf4j.Slf4j;import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;@Slf4j
@Service
public class WordServiceImpl  {@Overridepublic void export(HttpServletResponse response, CommonQuery query) {try {/*测试数据File file = new File("C:\\Users\\Licha\\Desktop\\模板.docx");InputStream ins = new FileInputStream(file);*///获取Word模板,模板存放路径在项目的resources目录下InputStream ins = this.getClass().getResourceAsStream("/template/模板.docx");IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins,TemplateEngineKind.Freemarker);IContext context = report.createContext();Vo vo = 获取写入数据方法(query);//创建要替换的文本变量// 字段 ${title}context.put("title" , vo.getTitle());//集合 -- ${list.type} context.put("list" , vo.listVos());//创建字段元数据FieldsMetadata fm = report.createFieldsMetadata();//Word模板中的表格数据对应的集合类型fm.load("list" , ListVo.class, true);/*//输出到本地目录FileOutputStream out = new FileOutputStream(new File("C:\\Users\\Licha\\Desktop\\数据.docx"));report.process(context, out);*/response.setCharacterEncoding("utf-8");response.setContentType("application/msword");String fileName = ledgerVo.getTitle() + ".docx";response.setHeader("Content-Disposition" , "attachment;filename=".concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));report.process(context, response.getOutputStream());} catch (IOException e) {log.error("IOException报错" , e);} catch (XDocReportException e) {log.error("XDocReportException报错" , e);} catch (Exception e) {log.error("Exception" , e);}}
}

参考资料

https://blog.csdn.net/plxddyxnmd/article/details/109129838 - 学习

https://www.cnblogs.com/huigee/p/16588297.html - 解决文件读取乱码bug

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

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

相关文章

基于B/S架构的医院一体化电子病历编辑器源码

电子病历在线制作、管理和使用的一体化电子病历解决方案&#xff0c;通过一体化的设计&#xff0c;提供对住院病人的电子病历书写、保存、修改、打印等功能。电子病历系统将临床医护需要的诊疗资料以符合临床思维的方法展示。建立以病人为中心&#xff0c;以临床诊疗信息为主线…

STM32USART+DMA实现不定长数据接收/发送

STM32USARTDMA实现不定长数据接收 CubeMX配置代码分享实践结果 这一期的内容是一篇代码分享&#xff0c;CubeMX配置介绍&#xff0c;关于基础的内容可以往期内容 夜深人静学32系列11——串口通信夜深人静学32系列18——DMAADC单/多通道采集STM32串口重定向/实现不定长数据接收 …

【Redis】Redis高级特性和应用(慢查询、Pipeline、事务、Lua)

目录 Redis的慢查询 慢查询配置 慢查询操作命令 慢查询建议 Pipeline 事务 Redis的事务原理 Redis的watch命令 Pipeline和事务的区别 Lua Lua入门 安装Lua Lua基本语法 注释 标示符 关键词 全局变量 Lua中的数据类型 Lua 中的函数 Lua 变量 Lua中的控制语句…

Windows 基于 VMware 虚拟机安装银河麒麟高级服务器操作系统

前言 抱着学习的态度研究一下麒麟系统的安装 银河麒麟&#xff08;KylinOS&#xff09;原是在“863计划”和国家核高基科技重大专项支持下&#xff0c;国防科技大学研发的操作系统&#xff0c;后由国防科技大学将品牌授权给天津麒麟&#xff0c;后者在2019年与中标软件合并为…

ERROR:sf is not compatible with GDAL version below 2.0.1

在安装monocle3时&#xff0c;出现报错信息如下&#xff1a; devtools::install_github(cole-trapnell-lab/monocle3) 显示GDAL版本不对&#xff0c;必须得更新到2.0.1以上&#xff0c;于是尝试更新版本。 sudo add-apt-repository -y ppa:ubuntugis/ppa sudo apt update su…

【linux网络】补充网关服务器搭建,综合应用SNAT、DNAT转换,dhcp分配、dns分离解析,nfs网络共享以及ssh免密登录

目录 linux网络的综合应用 1&#xff09;网关服务器&#xff1a;ens35&#xff1a;12.0.0.254/24&#xff0c;ens33&#xff1a;192.168.100.254/24&#xff1b;Server1&#xff1a;192.168.100.101/24&#xff1b;PC1和server2&#xff1a;自动获取IP&#xff1b;交换机无需…

Pandas进阶:transform 数据转换的常用技巧

引言 本次给大家介绍一个功能超强的数据处理函数transform&#xff0c;相信很多朋友也用过&#xff0c;这里再次进行详细分享下。 transform有4个比较常用的功能&#xff0c;总结如下&#xff1a; 转换数值 合并分组结果 过滤数据 结合分组处理缺失值 一. 转换数值 pd.…

Kafka的存储机制和可靠性

文章目录 前言一、Kafka 存储选择二、Kafka 存储方案剖析三、Kafka 存储架构设计四、Kafka 日志系统架构设计4.1、Kafka日志目录布局4.2、Kafka磁盘数据存储 五、Kafka 可靠性5.1、Producer的可靠性保证5.1.1、kafka 配置为 CP(Consistency & Partition tolerance)系统5.1.…

IDEA2023找不到 Allow parallel run

我的idea版本&#xff1a;2023.1.4 第一步&#xff1a;点击Edit Configrations 第二步&#xff1a;点击Modify options 第三步&#xff1a;勾选Allow multiple instances 最后点击Apply应用一下 ok,问题解决&#xff01;

中台战略思想与架构总结

中台战略思想与架构总结 在2015年年中&#xff0c;马云带领阿里高管&#xff0c;拜访了游戏公司Supercell&#xff0c;以《部落战争》《海岛奇兵》《卡通农场》等游戏知名。 Supercell是一家典型的以小团队模式进行游戏开发的公司&#xff0c;一般来说两个员工&#xff0c;或…

UG\NX二次开发 设置是否允许通过NXOpen锁定属性

文章作者:里海 来源网站:里海NX二次开发3000例专栏 感谢粉丝订阅 感谢 2301_80939425 订阅本专栏,非常感谢。 简介 在使用UF_ATTR_set_locked函数锁定属性前,需要先设置是否允许通过NXOpen锁定属性。使用下面的代码可以修改“用户默认设置”当 NX 启动时,客户默认值将读取…

代码随想录算法训练营 ---第五十一天

1.第一题&#xff1a; 简介&#xff1a; 本题相较于前几题状态复杂了起来&#xff0c;因为多了一个冷冻期。本题讲解可去代码随想录看&#xff0c;这里差不多只是加了些自己的理解。 动规五部曲&#xff0c;分析如下&#xff1a; 确定dp数组以及下标的含义 dp[i][j]&#x…