Spring Boot + FreeMarker 實現(xiàn)動態(tài)Word文檔導(dǎo)出功能
Spring Boot + FreeMarker 實現(xiàn)動態(tài)Word文檔導(dǎo)出
在現(xiàn)代企業(yè)應(yīng)用中,文檔自動化生成是一項提升工作效率的重要功能。Spring Boot與FreeMarker的組合,為開發(fā)者提供了一個強大的平臺,可以輕松實現(xiàn)動態(tài)Word文檔的導(dǎo)出。本文將指導(dǎo)你如何使用Spring Boot與FreeMarker模板引擎,創(chuàng)建一個簡單的應(yīng)用,用于根據(jù)數(shù)據(jù)庫數(shù)據(jù)動態(tài)生成Word文檔并下載。
技術(shù)棧簡介
- Spring Boot:簡化Spring應(yīng)用初始搭建以及開發(fā)過程的框架,提供了快速開發(fā)、運行、部署的解決方案。
- FreeMarker:一款用Java編寫的模板引擎,特別適合生成HTML、XML、RTF、Java源代碼等文本格式的輸出,當(dāng)然也包括Word文檔。
準(zhǔn)備工作
1. 創(chuàng)建Spring Boot項目
使用Spring Initializr創(chuàng)建一個新的Spring Boot項目,記得勾選Thymeleaf
(雖然我們用FreeMarker,但Thymeleaf依賴也會帶來Spring MVC的支持,便于后續(xù)配置)。
2. 添加FreeMarker依賴
在pom.xml
中加入FreeMarker的依賴。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
3. 配置FreeMarker
在application.properties
中配置FreeMarker的路徑。
spring.freemarker.template-loader-path=classpath:/templates/
創(chuàng)建FreeMarker模板
在src/main/resources/templates
目錄下,創(chuàng)建一個名為document.ftl
的FreeMarker模板文件,內(nèi)容如下:
<!DOCTYPE html> <html> <head> <#assign document = { "title": "動態(tài)Word文檔", "content": [ {"header": "章節(jié)一", "text": "這里是章節(jié)一的內(nèi)容..."}, {"header": "章節(jié)二", "text": "這里是章節(jié)二的內(nèi)容..."} ] }> </head> <body> <h1>${document.title}</h1> <#list document.content as section> <h2>${section.header}</h2> <p>${section.text}</p> </#list> </body> </html>
編寫Controller
創(chuàng)建一個Controller來處理請求,讀取模板并填充數(shù)據(jù),最后將Word文檔返回給用戶下載。
java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import freemarker.template.Configuration; import freemarker.template.Template; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import java.io.IOException; import java.util.HashMap; import java.util.Map; @RestController public class DocumentController { @Autowired private Configuration freemarkerConfig; @GetMapping("/export") public ResponseEntity<byte[]> exportDocument() throws IOException { // 假設(shè)數(shù)據(jù)是從數(shù)據(jù)庫獲取的,這里為了簡化直接構(gòu)造示例數(shù)據(jù) Map<String, Object> dataModel = new HashMap<>(); dataModel.put("title", "來自數(shù)據(jù)庫的標(biāo)題"); dataModel.put("content", getDatabaseContent()); Template template = freemarkerConfig.getTemplate("document.ftl"); String processedHtml = FreeMarkerTemplateUtils.processTemplateIntoString(template, dataModel); // 將HTML轉(zhuǎn)換為Word文檔(此處簡化處理,實際可能需要使用Apache POI等庫) byte[] wordBytes = convertHtmlToWord(processedHtml); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", "document.docx"); return new ResponseEntity<>(wordBytes, headers, HttpStatus.OK); } private List<Map<String, String>> getDatabaseContent() { // 此處模擬從數(shù)據(jù)庫獲取數(shù)據(jù) return Arrays.asList( Map.of("header", "數(shù)據(jù)庫章節(jié)一", "text", "數(shù)據(jù)庫內(nèi)容一"), Map.of("header", "數(shù)據(jù)庫章節(jié)二", "text", "數(shù)據(jù)庫內(nèi)容二") ); } // 簡化的HTML轉(zhuǎn)Word邏輯,實際應(yīng)用中可能需要更復(fù)雜的轉(zhuǎn)換過程 private byte[] convertHtmlToWord(String html) { // 這里省略了HTML轉(zhuǎn)Word的具體實現(xiàn),可以使用第三方庫如Apache POI等 return html.getBytes(); // 這只是示例,實際返回的應(yīng)該是Word文檔的字節(jié)流 } }
總結(jié)
通過上述步驟,我們使用Spring Boot與FreeMarker成功構(gòu)建了一個簡單的應(yīng)用,能夠根據(jù)模板和數(shù)據(jù)庫數(shù)據(jù)動態(tài)生成Word文檔并提供下載。實際應(yīng)用中,你可能需要引入額外的庫(如Apache POI等)來更精確地控制Word文檔的樣式和結(jié)構(gòu),以及更高效地處理HTML到Word的轉(zhuǎn)換過程。此外,確保處理好模板的安全性,避免注入攻擊等問題。
到此這篇關(guān)于Spring Boot + FreeMarker 實現(xiàn)動態(tài)Word文檔導(dǎo)出的文章就介紹到這了,更多相關(guān)Spring Boot FreeMarker Word文檔導(dǎo)出內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java json字符串轉(zhuǎn)JSONObject和JSONArray以及取值的實例
這篇文章主要介紹了java json字符串轉(zhuǎn)JSONObject和JSONArray以及取值的實例的相關(guān)資料,需要的朋友可以參考下2017-05-05基于注解的springboot+mybatis的多數(shù)據(jù)源組件的實現(xiàn)代碼
這篇文章主要介紹了基于注解的springboot+mybatis的多數(shù)據(jù)源組件的實現(xiàn),會使用到多個數(shù)據(jù)源,文中通過代碼講解的非常詳細,需要的朋友可以參考下2021-04-04SpringBoot中的static靜態(tài)資源訪問、參數(shù)配置、代碼自定義訪問規(guī)則詳解
這篇文章主要介紹了SpringBoot的static靜態(tài)資源訪問、參數(shù)配置、代碼自定義訪問規(guī)則,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07