Springboot整合poi實(shí)現(xiàn)批量修改word文件內(nèi)容
一、POI是什么?
Apache POI 是用Java編寫的免費(fèi)開源的跨平臺(tái)的 Java API,它提供API給Java程式對(duì)Microsoft Office格式檔案讀和寫的功能。
本文主要使用POI操作XWPFDocument對(duì)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項(xiàng)目 --> <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);
// 直接調(diào)用 XWPFRun 的 setText() 方法設(shè)置文本時(shí),在底層會(huì)重新創(chuàng)建一個(gè) XWPFRun,把文本附加在當(dāng)前文本后面,
// 所以我們不能直接設(shè)值,需要先刪除當(dāng)前 run, 然后再自己手動(dòng)插入一個(gè)新的 run
paragraph.removeRun(i1);
paragraph.insertNewRun(i1).setText(runText);
}
}
}
//獲得所有的表格 ------ 替換表格文字
List<XWPFTable> tables = doc.getTables();
for (XWPFTable table : tables) {
//一個(gè)表格包含多行
List<XWPFTableRow> rows = table.getRows();
for (XWPFTableRow row : rows) {
//一行包含多列
List<XWPFTableCell> tableCells = row.getTableCells();
//一行一列,橫縱坐標(biāo)確定一個(gè) XWPFTableCell
//表格中的一格相當(dāng)于一個(gè)沒有頁(yè)眉和頁(yè)腳的文檔
for (XWPFTableCell tableCell : tableCells) {
String text = tableCell.getText();
if (!StringUtils.isEmpty(text) && Objects.equals(text, source)) {
//setText底層是追加,所以要?jiǎng)h除刪除原來的段落
tableCell.removeParagraph(0);
//設(shè)置段落的樣式,行間距
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 +"不是一個(gè)目錄。");
}
return "replaceWord";
}
}2.3 replaceWord.html文件
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>批量修改word文字 只測(cè)試過docx</title>
</head>
<body>
<form method="get" action="/replaceWord">
<label>
選擇導(dǎo)入文件夾:
<input type="text" th:name="input" required>
</label>
<label>
選擇輸出文件夾:
<input type="text" th:name="output" required>
</label>
<label>
需要修改的內(nèi)容:
<input type="text" th:name="source" required>
</label>
<label>
修改為什么內(nèi)容:
<input type="text" th:name="target" required>
</label>
<button type="submit">進(jìn)行替換</button>
</form>
</body>
</html>訪問前端項(xiàng)目:

總結(jié)
以上案例實(shí)現(xiàn)了批量替換文件夾下同級(jí)的.docx文件中的內(nèi)容,案例是暴露給其他用戶使用所以簡(jiǎn)單寫了個(gè)html,開發(fā)者可直接用main函數(shù)調(diào)用即可。
到此這篇關(guān)于Springboot整合poi實(shí)現(xiàn)批量修改word文件內(nèi)容的文章就介紹到這了,更多相關(guān)Springboot修改word文件內(nèi)容內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot使用Apache?POI實(shí)現(xiàn)導(dǎo)出Word和PPT的完整代碼
- SpringBoot項(xiàng)目中使用EasyPOI方式導(dǎo)出合同Word文檔的代碼實(shí)現(xiàn)
- springboot集成easypoi導(dǎo)出word換行處理過程
- SpringBoot+EasyPOI輕松實(shí)現(xiàn)Excel和Word導(dǎo)出PDF
- SpringBoot集成Poi-tl實(shí)現(xiàn)動(dòng)態(tài)Word文檔生成的詳細(xì)步驟
- SpringBoot+POI實(shí)現(xiàn)給word添加水印功能
- SpringBoot調(diào)用Poi-tl實(shí)現(xiàn)渲染數(shù)據(jù)并生成Word文檔
- SpringBoot如何基于POI-tl和word模板導(dǎo)出龐大的Word文件
- Java?SpringBoot集成文件之如何使用POI導(dǎo)出Word文檔
相關(guān)文章
ssm框架下web項(xiàng)目,web.xml配置文件的作用(詳解)
下面小編就為大家?guī)硪黄猻sm框架下web項(xiàng)目,web.xml配置文件的作用(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
SpringBoot中的yaml語法及靜態(tài)資源訪問問題
這篇文章主要介紹了SpringBoot中的yaml語法及靜態(tài)資源訪問問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07
SpringMVC攔截器實(shí)現(xiàn)登錄認(rèn)證
這篇文章主要介紹了SpringMVC攔截器實(shí)現(xiàn)登錄認(rèn)證的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11

