自貢做網(wǎng)站的公司百度官網(wǎng)認(rèn)證申請
Spring Boot + FreeMarker 實(shí)現(xiàn)動態(tài)Word文檔導(dǎo)出
在現(xiàn)代企業(yè)應(yīng)用中,文檔自動化生成是一項(xiàng)提升工作效率的重要功能。Spring Boot與FreeMarker的組合,為開發(fā)者提供了一個(gè)強(qiáng)大的平臺,可以輕松實(shí)現(xiàn)動態(tài)Word文檔的導(dǎo)出。本文將指導(dǎo)你如何使用Spring Boot與FreeMarker模板引擎,創(chuàng)建一個(gè)簡單的應(yīng)用,用于根據(jù)數(shù)據(jù)庫數(shù)據(jù)動態(tài)生成Word文檔并下載。
技術(shù)棧簡介
- Spring Boot:簡化Spring應(yīng)用初始搭建以及開發(fā)過程的框架,提供了快速開發(fā)、運(yùn)行、部署的解決方案。
- FreeMarker:一款用Java編寫的模板引擎,特別適合生成HTML、XML、RTF、Java源代碼等文本格式的輸出,當(dāng)然也包括Word文檔。
準(zhǔn)備工作
1. 創(chuàng)建Spring Boot項(xiàng)目
使用Spring Initializr創(chuàng)建一個(gè)新的Spring Boot項(xiàng)目,記得勾選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)建一個(gè)名為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)建一個(gè)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文檔(此處簡化處理,實(shí)際可能需要使用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邏輯,實(shí)際應(yīng)用中可能需要更復(fù)雜的轉(zhuǎn)換過程
private byte[] convertHtmlToWord(String html) {// 這里省略了HTML轉(zhuǎn)Word的具體實(shí)現(xiàn),可以使用第三方庫如Apache POI等return html.getBytes(); // 這只是示例,實(shí)際返回的應(yīng)該是Word文檔的字節(jié)流
}
}
總結(jié)
通過上述步驟,我們使用Spring Boot與FreeMarker成功構(gòu)建了一個(gè)簡單的應(yīng)用,能夠根據(jù)模板和數(shù)據(jù)庫數(shù)據(jù)動態(tài)生成Word文檔并提供下載。實(shí)際應(yīng)用中,你可能需要引入額外的庫(如Apache POI等)來更精確地控制Word文檔的樣式和結(jié)構(gòu),以及更高效地處理HTML到Word的轉(zhuǎn)換過程。此外,確保處理好模板的安全性,避免注入攻擊等問題。