spring boot 动态docx
时间: 2023-10-09 18:14:55 浏览: 61
Spring Boot是一个快速开发微服务的框架,而docx是一种常见的办公文档格式。如果你需要在Spring Boot应用中动态生成docx文档,可以使用Apache POI和Freemarker来实现。
Apache POI是一个用于操作Microsoft Office文档的Java API,包括Word、Excel、PowerPoint等。而Freemarker是一个模板引擎,可以将数据和模板进行结合生成文档。
下面是一个简单的示例,演示如何使用Apache POI和Freemarker生成docx文档:
1. 添加依赖
在Spring Boot项目的pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
```
2. 创建模板
在src/main/resources目录下创建一个名为template.docx的模板文件,用于定义docx文档的格式和样式。可以使用Microsoft Word等工具创建模板,然后将其保存为docx格式。
3. 创建Controller
在Spring Boot应用中创建一个Controller,用于处理HTTP请求并生成docx文档。以下示例中,我们假设需要生成一份简历文档,需要填充姓名、年龄、电话等信息。
```
@RestController
public class ResumeController {
@GetMapping("/resume")
public ResponseEntity<byte[]> generateResume() throws IOException, TemplateException {
// 读取模板文件
File templateFile = new File(getClass().getClassLoader().getResource("template.docx").getFile());
FileInputStream inputStream = new FileInputStream(templateFile);
XWPFDocument document = new XWPFDocument(OPCPackage.open(inputStream));
// 准备数据模型
Map<String, Object> dataModel = new HashMap<>();
dataModel.put("name", "张三");
dataModel.put("age", 25);
dataModel.put("phone", "13800138000");
// 渲染模板并替换文本
Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
configuration.setDefaultEncoding("UTF-8");
StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
stringTemplateLoader.putTemplate("document", IOUtils.toString(inputStream));
configuration.setTemplateLoader(stringTemplateLoader);
Template template = configuration.getTemplate("document");
StringWriter writer = new StringWriter();
template.process(dataModel, writer);
// 替换文档内容
XWPFDocument newDocument = new XWPFDocument(new ByteArrayInputStream(writer.toString().getBytes(StandardCharsets.UTF_8)));
document.getParagraphs().forEach(paragraph -> {
List<XWPFRun> runs = paragraph.getRuns();
runs.forEach(run -> {
String text = run.text();
if (text != null && text.contains("${")) {
run.setText(text.replace("${", "").replace("}", ""), 0);
}
});
});
document.getTables().forEach(table -> {
table.getRows().forEach(row -> {
row.getTableCells().forEach(cell -> {
List<XWPFParagraph> paragraphs = cell.getParagraphs();
paragraphs.forEach(paragraph -> {
List<XWPFRun> runs = paragraph.getRuns();
runs.forEach(run -> {
String text = run.text();
if (text != null && text.contains("${")) {
run.setText(text.replace("${", "").replace("}", ""), 0);
}
});
});
});
});
});
// 生成docx文件
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
document.write(outputStream);
byte[] bytes = outputStream.toByteArray();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", "resume.docx");
return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
}
}
```
4. 启动应用
在Spring Boot应用启动后,访问http://localhost:8080/resume即可下载生成的docx文档。
以上就是一个简单的Spring Boot动态生成docx文档的示例,可以根据实际需求进行调整和优化。