1. 准备环境
1.1 word版本:MacOS系统下的Office360
word for Mac下好多操作和win的不一样啊啊啊,差点栽在Mac word的模版设计上。
win版本的word模版设计请参考文章:点此参考原文章
1.2 开发环境
开发语言:Java 8
开发工具:idea
第三方依赖:XDocReport、POI、Freemarker
模版语言:Freemarker
2. Word模版设计
2.1 示例目标word模版
2.2 word模版制作-插入域
将需要动态写入的内容用word域框起来。
1. 普通字段插入
word域(Mac版)插入方式如下:
2. 表格类型(列表)域插入
遍历列表输出,需要将域代码设置为${集合.字段名}
,如${repayment.payDate}
,${repayment.interest}
。
2.3 替换所有需要填充域的内容
接下来将所有内容替换为域代码
2.4 模版导出
将设置好的word模版导出为xml格式。
注意,如果有list数据,需要在导出的xml模版里再加入两行代码:
3. Java代码
3.1 引入依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls</artifactId>
<version>2.6.0</version>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>1.2.0</version>
</dependency>
<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>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.2 编写导出文档代码
/**
* 使用FreeMarker自动生成Word文档
*
* @param dataMap 生成Word文档所需要的数据,将文档中的域代码的名字,作为param的key,对应变量的数据为value
* @param fileName 生成Word文档的全路径名称
*/
public static void generateWord(Map<String, Object> dataMap, String fileName, String templatePath, String template) throws Exception {
// 设置FreeMarker的版本和编码格式
Configuration configuration = new Configuration(new Version("2.3.23"));
configuration.setDefaultEncoding("UTF-8");
// 设置FreeMarker生成Word文档所需要的模板的路径
configuration.setDirectoryForTemplateLoading(new File(templatePath));
// 设置FreeMarker生成Word文档所需要的模板
Template t = configuration.getTemplate(template, "UTF-8");
// 创建一个Word文档的输出流
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName)), "UTF-8"));
//FreeMarker使用Word模板和数据生成Word文档
t.process(dataMap, out);
out.flush();
out.close();
}
3.3 测试
@Test
public void test() throws Exception {
FinancialHelperServiceImpl financialHelperService = new FinancialHelperServiceImpl();
List<RepaymentVo> repayments = financialHelperService.getInterestPlan(1000000,4,36,
LocalDate.now(),3,0,0);
repayments.forEach(System.out::println);
Map<String, Object> params = new HashMap<>();
params.put("repayments", repayments);
params.put("amount",100);
params.put("rate",6);
params.put("duration",40);
params.put("conStart","2022-05-01");
params.put("methodOfRepayment",1);
params.put("totalInterest", 110);
String fileName="test.doc";
generateWord(params, "src/main/resources/templates/" + fileName, "src/main/resources/templates/", "还款计划模版.xml");
}