Apache POI 是用Java编写的免费开源的跨平台的 Java API,它提供API给Java程式对Microsoft Office格式档案读和写的功能。
本文主要使用POI操作XWPFDocument对word文档的读写。
相关链接:
1、Apache POI Word(docx)的简短示例教程http://deepoove.com/poi-tl/apache-poi-guide.html
2、poi-tl在生成的文档中会完美保留模板中的样式,还可以为标签设置样式,标签的样式会被应用到替换后的文本上,可以专注于模板设计。
http://deepoove.com/poi-tl/

代码如下(示例):
org.apache.poi poi 5.0.0
org.apache.poi poi-ooxml 5.0.0
org.springframework.boot spring-boot-starter-web
org.springframework.boot spring-boot-starter-thymeleaf
@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 paragraphs = doc.getParagraphs();for (XWPFParagraph paragraph : paragraphs) {if(!paragraph.getParagraphText().contains(source)){continue;}//获取段落中的runsList 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, 然后再自己手动插入一个新的 runparagraph.removeRun(i1);paragraph.insertNewRun(i1).setText(runText);}}}//获得所有的表格 ------ 替换表格文字List tables = doc.getTables();for (XWPFTable table : tables) {//一个表格包含多行List rows = table.getRows();for (XWPFTableRow row : rows) {//一行包含多列List 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";}
}
批量修改word文字 只测试过docx
访问前端项目:

以上案例实现了批量替换文件夹下同级的.docx文件中的内容,案例是暴露给其他用户使用所以简单写了个html,开发者可直接用main函数调用即可。