雖然 Apache POI 是非常強大的針對 Microsoft Office 讀寫的工具,但是其太過于復雜,往往不太好編寫代碼,且編寫出的代碼不宜維護(也可能只是網上大家分享的代碼大多都不好好整理的原因)。所以在導出讀取 Excel 方面有阿里簡化后的 easyexcel 可供使用,關于導出 word 我們可以使用 freemarker 模板引擎。
1、在 springboot 項目中添加 freemarker 的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2、使用 word 做一個你需要的文件模板,并將其中需要改變的部分編程變量(注:圖片建議先貼上大小相等的其他圖片),類似下面:
3、制作完成之后,將其另存為xml格式
4、將轉存的 xml 文件使用文本編輯器例如 notepad++ 等打開,將其中紅色區域的刪除掉,那些是圖片的base64編碼后的信息,然后替換為一個圖片變量,傳入新的圖片的 base64 編碼即可導出你所要的圖片,一般來說導出的圖片都是由前臺 echarts 等工具生成的。
5、導出列表的話還需要在 xml 中找到 <w:tr> ,在其前加入 <#list list as list> (注意第一個list 不能變,第二個list 為變量名,第三個list 為別名),和</w:tr> 后加入 </#list> ,此外需將 name 改成 list.name,gender 改成 list.gender 。
6、復制 xml 粘貼至項目文件中,并將后綴名更改為 .ftl
7、編寫導出 Word 工具類
@Slf4j
public class ExportWordUtil {
private Configuration config;
public ExportWordUtil() {
config = new Configuration(DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
config.setDefaultEncoding("utf-8");
}
/**
* FreeMarker生成Word
* @param dataMap 數據
* @param templateName 模板名
* @param response HttpServletResponse
* @param fileName 導出的word文件名
*/
public void exportWord(Map<String, Object> dataMap, String templateName, HttpServletResponse response, String fileName) {
//加載模板(路徑)數據,也可使用setServletContextForTemplateLoading()方法放入到web文件夾下
config.setClassForTemplateLoading(this.getClass(), "/templates");
//設置異常處理器 這樣的話 即使沒有屬性也不會出錯 如:${list.name}...不會報錯
config.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
try {
if(templateName.endsWith(".ftl")) {
templateName = templateName.substring(0, templateName.indexOf(".ftl"));
}
Template template = config.getTemplate(templateName + ".ftl");
response.setContentType("application/msword");
response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(fileName, StandardCharsets.UTF_8));
OutputStream outputStream = response.getOutputStream();
Writer out = new BufferedWriter(new OutputStreamWriter(outputStream));
//將模板中的預先的代碼替換為數據
template.process(dataMap, out);
log.info("由模板文件:" + templateName + ".ftl" + " 生成Word文件 " + fileName + " 成功??!");
out.flush();
} catch (TemplateNotFoundException e) {
log.info("模板文件未找到");
e.printStackTrace();
} catch (MalformedTemplateNameException e) {
log.info("模板類型不正確");
e.printStackTrace();
} catch (TemplateException e) {
log.info("填充模板時異常");
e.printStackTrace();
} catch (IOException e) {
log.info("IO異常");
e.printStackTrace();
}
}
}
8、編寫簡單的Controller方法
@RestController
public class Controller {
/**
* @param image 注意該image參數為前臺傳過來圖片
*/
@PostMapping("/exportWord")
public void exportWord(HttpServletResponse response, @RequestParam("image") MultipartFile image){
ExportWordUtil ewUtil = new ExportWordUtil();
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("title", "荒野大鏢客2人物");
Base64.Encoder base64 = Base64.getEncoder();
try {
dataMap.put("image", base64.encodeToString(image.getBytes()));
} catch (Exception e){
}
List<User> userList = new ArrayList<>();
userList.add(new User("亞當·摩根","男"));
userList.add(new User("達奇","男"));
userList.add(new User("阿比蓋爾·馬斯頓","女"));
dataMap.put("list", userList);
ewUtil.exportWord(dataMap, "test.ftl", response, "荒野大鏢客2人物.doc");
}
}
9、application.yml 中設置下 size,防止圖片的字節數過大
spring:
servlet:
multipart:
max-file-size: 1024MB
max-request-size: 1024MB
10、啟動項目,傳入某個圖片進行測試【注意:盡量前臺直接傳圖片,而不是傳圖片的 base64 編碼的字符串,這可能導致 word 中圖片顯示不完整】
代碼地址:https://github.com/Econfer/exportword
參考如下: