一、修改pom
1.引入依赖
<!-- freemarker文档处理器 开始 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
<version>2.4.3</version>
</dependency>
<!-- freemarker文档处理器 结束 -->
2.修改打包文件类型,添加ftl文件
<build>
<!--项目相关的所有资源路径列表,例如和项目相关的配置文件、属性文件,这些资源被包含在最终的打包文件里。 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.yml</include>
<include>**/*.xml</include>
<include>**/*.ftl</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
二、准备模板
1.准备word文档的文件后缀为doc的模板文件如下:
注:本文介绍的是导出doc格式文档,docx与此不同请注意,如果只是简单处理,docx可另存为doc格式文件。
2.将doc文档另存为xml文件
注:选择2003版
3.将xml文件重命名后缀改为ftl
4.将ftl文件引入项目resource资源下,如图:
5.替换ftl文件中变量占位符
注:${name!“”},其中name为变量key,如果key有对应value值,则使用value值,如果value为null则使用空字符串(“”)占位,避免空指针。
注:ftl文件解析时可能会把一个字段拆分为多个字段展示,需要手动合并到一起,如下:
三、工具类
1、IO工具类
package com.bs.utils;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
/**
* @author admin
* @description IO工具类
* @createDate 2022/6/22
*/
@Slf4j
public class IOUtils {
/**
* 关闭InputStream
*
* @param stream InputStream
*/
public static void close(InputStream stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
log.error("close stream error", e);
}
}
}
/**
* 关闭OutputStream
*
* @param stream OutputStream
*/
public static void close(OutputStream stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
log.error("close stream error", e);
}
}
}
/**
* 关闭Reader
*
* @param stream Reader
*/
public static void close(Reader stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
log.error("close stream error", e);
}
}
}
/**
* 关闭Writer
*
* @param stream Writer
*/
public static void close(Writer stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
log.error("close stream error", e);
}
}
}
}
2.web工具类
package com.bs.utils;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletResponse;
/**
* @author admin
* @description Web工具类
* @createDate 2022/6/22
*/
public class WebUtils {
/**
* 获取Response
*
*/
public static HttpServletResponse getResponse() {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
Assert.notNull(requestAttributes, "requestAttributes is null");
return requestAttributes.getResponse();
}
}
3.导出doc文档工具类
package com.bs.utils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;
/**
* @author admin
* @description
* @createDate 2022/6/21
*/
@Slf4j
public class WordUtils {
/**
* 文件分隔符
*/
public static String FILE_DELIMITER = "/";
/**
* 模板FTL文件后缀
*/
public static String FTL_SUFFIX = ".ftl";
/**
* 生成DOC文件后缀
*/
public static String DOC_SUFFIX = "_%s.doc";
public static void exportDocWordByWeb(String fileNamePrefix, String templateFilePath, Map<String, Object> dataMap) {
// 获取模板文件名称
String templateFileName = templateFilePath.substring(templateFilePath.lastIndexOf(FILE_DELIMITER) + 1);
// 获取当前日期
String currentDate = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
// 处理导出后文件名称
String fileName = templateFileName.replace(FTL_SUFFIX, String.format(DOC_SUFFIX, currentDate));
if (StringUtils.isNotBlank(fileNamePrefix)) {
fileName = String.format("%s_%s", fileNamePrefix, fileName);
}
// 文件名称使用UTF_8编码
String encodeFileName;
try {
encodeFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
log.error("编码文件名失败,文件名称{}", fileName);
log.error(e.getMessage(), e.getCause());
throw new RuntimeException("编码文件名失败");
}
InputStream templateFileInputStream = null;
InputStreamReader inputStreamReader = null;
ByteArrayOutputStream byteArrayOutputStream = null;
OutputStreamWriter outputStreamWriter = null;
ByteArrayInputStream byteArrayInputStream = null;
OutputStream outputStream = null;
try {
templateFileInputStream = WordUtils.class.getClassLoader().getResourceAsStream(templateFilePath);
Assert.notNull(templateFileInputStream, String.format("模板文件不存在,路径:%s", templateFilePath));
Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
configuration.setDefaultEncoding(StandardCharsets.UTF_8.name());
inputStreamReader = new InputStreamReader(templateFileInputStream);
Template template = new Template(templateFileName, inputStreamReader, configuration);
byteArrayOutputStream = new ByteArrayOutputStream();
outputStreamWriter = new OutputStreamWriter(byteArrayOutputStream);
template.process(dataMap, outputStreamWriter);
// 导出文件
HttpServletResponse response = WebUtils.getResponse();
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
response.setContentType("application/msword");
response.setHeader("Content-Disposition", "attachment;filename=" + encodeFileName);
outputStream = response.getOutputStream();
byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
byte[] buffer = new byte[1024];
int count;
while ((count = byteArrayInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, count);
}
} catch (IOException | TemplateException e) {
log.error(e.getMessage(), e.getCause());
throw new RuntimeException("文件导出异常");
} finally {
// 关闭流
IOUtils.close(outputStream);
IOUtils.close(byteArrayInputStream);
IOUtils.close(outputStreamWriter);
IOUtils.close(byteArrayOutputStream);
IOUtils.close(inputStreamReader);
IOUtils.close(templateFileInputStream);
}
}
}
四、测试
package com.bs.controller;
import com.bs.utils.WordUtils;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
@Api(tags = "AdminController", description = "管理员管理")
@RestController
@RequestMapping("/admin")
public class AdminController {
@GetMapping("exportDocWord")
@ApiOperation("导出Doc文档")
public void exportDocWord() {
HashMap<String, Object> dataMap = Maps.newHashMap();
dataMap.put("type", "doc");
dataMap.put("name", "李四");
// 为了模拟控制针,age不存值
String templateFilePath = "template/word/测试文档.ftl";
WordUtils.exportDocWordByWeb("导出", templateFilePath, dataMap);
}
}
五、测试结果