Springboot整合poi实现批量修改word文件内容
一、POI是什么?
Apache POI 是用Java编写的免费开源的跨平台的 Java API,它提供API给Java程式对Microsoft Office格式档案读和写的功能。
本文主要使用POI操作XWPFDocument对word文档的读写。
二、XWPFDocument属性介绍

2.1 pom.xml 文件
代码如下(示例):
<!--poi依赖--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.0.0</version> </dependency> <!-- web项目 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--thymeleaf模板引擎--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2.2 控制层
@Controller
public class TestController {
@GetMapping("/")
public String test(){
return "replaceWord";
}
@GetMapping("/replaceWord")
public String inputParam(String input,String output,String source,String target) throws IOException {
File file = new File(input);
File readFile;
if(file.isDirectory()) {
System.out.println("正在读取"+ input +"目录....");
String[] list = file.list();
for(int i = 0; i< Objects.requireNonNull(list).length; i++) {
readFile = new File(input +"/"+list[i]);
if(readFile.isDirectory()) {
System.out.println("文件夹:"+list[i]);
}else {
System.out.println("正在读取"+ input + "/" + list[i]);
FileInputStream in = new FileInputStream(readFile);
XWPFDocument doc = new XWPFDocument(in);
//正文段落-获取所有段落 ======== 替换段落文字
List<XWPFParagraph> paragraphs = doc.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
if(!paragraph.getParagraphText().contains(source)){
continue;
}
//获取段落中的runs
List<XWPFRun> runs = paragraph.getRuns();
for (int i1 = 0; i1 < runs.size(); i1++) {
XWPFRun run = runs.get(i1);
String runText = run.toString();
if(runText.contains(source)){
runText = runText.replaceAll(source, target);
// 直接调用 XWPFRun 的 setText() 方法设置文本时,在底层会重新创建一个 XWPFRun,把文本附加在当前文本后面,
// 所以我们不能直接设值,需要先删除当前 run, 然后再自己手动插入一个新的 run
paragraph.removeRun(i1);
paragraph.insertNewRun(i1).setText(runText);
}
}
}
//获得所有的表格 ------ 替换表格文字
List<XWPFTable> tables = doc.getTables();
for (XWPFTable table : tables) {
//一个表格包含多行
List<XWPFTableRow> rows = table.getRows();
for (XWPFTableRow row : rows) {
//一行包含多列
List<XWPFTableCell> tableCells = row.getTableCells();
//一行一列,横纵坐标确定一个 XWPFTableCell
//表格中的一格相当于一个没有页眉和页脚的文档
for (XWPFTableCell tableCell : tableCells) {
String text = tableCell.getText();
if (!StringUtils.isEmpty(text) && Objects.equals(text, source)) {
//setText底层是追加,所以要删除删除原来的段落
tableCell.removeParagraph(0);
//设置段落的样式,行间距
XWPFParagraph para = tableCell.addParagraph();
para.setSpacingBetween(1);
//赋值
tableCell.setText(target);
}
}
}
}
OutputStream out = Files.newOutputStream(Paths.get(output+"/"+list[i]));
// 输出
doc.write(out);
System.out.println("正在输出文件:" + output + "/" + list[i]);
in.close();
out.close();
}
}
}else {
System.out.println(input +"不是一个目录。");
}
return "replaceWord";
}
}2.3 replaceWord.html文件
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>批量修改word文字 只测试过docx</title>
</head>
<body>
<form method="get" action="/replaceWord">
<label>
选择导入文件夹:
<input type="text" th:name="input" required>
</label>
<label>
选择输出文件夹:
<input type="text" th:name="output" required>
</label>
<label>
需要修改的内容:
<input type="text" th:name="source" required>
</label>
<label>
修改为什么内容:
<input type="text" th:name="target" required>
</label>
<button type="submit">进行替换</button>
</form>
</body>
</html>访问前端项目:

总结
以上案例实现了批量替换文件夹下同级的.docx文件中的内容,案例是暴露给其他用户使用所以简单写了个html,开发者可直接用main函数调用即可。
到此这篇关于Springboot整合poi实现批量修改word文件内容的文章就介绍到这了,更多相关Springboot修改word文件内容内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
- SpringBoot使用Apache POI实现导出Word和PPT的完整代码
- SpringBoot项目中使用EasyPOI方式导出合同Word文档的代码实现
- springboot集成easypoi导出word换行处理过程
- SpringBoot+EasyPOI轻松实现Excel和Word导出PDF
- SpringBoot集成Poi-tl实现动态Word文档生成的详细步骤
- SpringBoot+POI实现给word添加水印功能
- SpringBoot调用Poi-tl实现渲染数据并生成Word文档
- SpringBoot如何基于POI-tl和word模板导出庞大的Word文件
- Java SpringBoot集成文件之如何使用POI导出Word文档
相关文章
SpringBoot中将@Bean方法解析为BeanDefinition详解
这篇文章主要介绍了SpringBoot中将@Bean方法解析为BeanDefinition详解,得到的BeanDefinition是ConfigurationClassBeanDefinition类型,会为BeanDefinition设置factoryMethodName,这意味着当实例化这个bean的时候将采用工厂方法,需要的朋友可以参考下2023-12-12
SpringBoot中文件下载与JSON响应冲突的深度解析与解决方案
在Spring Boot开发中,我们经常遇到需要同时处理文件下载和JSON响应的场景,很多开发者会遇到这样一个困惑:明明已经正确配置了文件下载,但浏览器要么无法下载文件,要么返回的JSON数据混乱,本文将深入分析这个问题的本质,并提供多种优雅的解决方案2026-03-03
Spring中的@ModelAttribute模型属性绑定详解
这篇文章主要介绍了Spring中的@ModelAttribute模型属性绑定详解,@ModelAttribute用于将方法参数或返回值绑定到Model属性上,并公开给Web视图,支持使用@RequestMapping注释的Controller类,需要的朋友可以参考下2024-02-02


最新评论