java?poi之XWPFDocument如何讀取word內(nèi)容并創(chuàng)建新的word
更新時間:2025年04月19日 11:31:21 作者:L-960
這篇文章主要介紹了java?poi之XWPFDocument如何讀取word內(nèi)容并創(chuàng)建新的word問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
Poi的Word文檔結(jié)構(gòu)介紹
1、poi之word文檔結(jié)構(gòu)介紹之正文段落
一個文檔包含多個段落,一個段落包含多個Runs,一個Runs包含多個Run,Run是文檔的最小單元
- 獲取所有段落:List paragraphs = word.getParagraphs();
- 獲取一個段落中的所有Runs:List xwpfRuns = xwpfParagraph.getRuns();
- 獲取一個Runs中的一個Run:XWPFRun run = xwpfRuns.get(index);
2、poi之word文檔結(jié)構(gòu)介紹之正文表格
一個文檔包含多個表格,一個表格包含多行,一行包含多列(格),每一格的內(nèi)容相當(dāng)于一個完整的文檔
- 獲取所有表格:List xwpfTables = doc.getTables();
- 獲取一個表格中的所有行:List xwpfTableRows = xwpfTable.getRows();
- 獲取一行中的所有列:List xwpfTableCells = xwpfTableRow.getTableCells();
- 獲取一格里的內(nèi)容:List paragraphs = xwpfTableCell.getParagraphs();
之后和正文段落一樣
注:
- 表格的一格相當(dāng)于一個完整的docx文檔,只是沒有頁眉和頁腳。里面可以有表格,使用xwpfTableCell.getTables()獲取,and so on
- 在poi文檔中段落和表格是完全分開的,如果在兩個段落中有一個表格,在poi中是沒辦法確定表格在段落中間的。(當(dāng)然除非你本來知道了,這句是廢話)。只有文檔的格式固定,才能正確的得到文檔的結(jié)構(gòu)
3、poi之word文檔結(jié)構(gòu)介紹之頁眉
一個文檔可以有多個頁眉(不知道怎么會有多個頁眉。。。),頁眉里面可以包含段落和表格
- 獲取文檔的頁眉:List headerList = doc.getHeaderList();
- 獲取頁眉里的所有段落:List paras = header.getParagraphs();
- 獲取頁眉里的所有表格:List tables = header.getTables();
之后就一樣了
4、poi之word文檔結(jié)構(gòu)介紹之頁腳
頁腳和頁眉基本類似,可以獲取表示頁數(shù)的角標(biāo)
IBodyElement-------------------迭代器(段落和表格)XWPFComment-------------------評論(個人理解應(yīng)該是批注)XWPFSDTXWPFFooter-------------------頁腳XWPFFootnotes-------------------腳注XWPFHeader-------------------頁眉XWPFHyperlink-------------------超鏈接XWPFNumbering-------------------編號XWPFParagraph-------------------段落XWPFPictureData-------------------圖片XWPFStyles-------------------樣式(設(shè)置多級標(biāo)題的時候用)XWPFTable-------------------表格
pom依賴
<dependencies>
<!--解析doc文檔HWPFDocument-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>springframework</groupId>
<artifactId>spring-core</artifactId>
<version>1.2.6</version>
</dependency>
</dependencies>代碼
- maven項目

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1;
import java.io.*;
import java.util.List;
public class poi3 {
public static void main(String[] args) throws IOException {
// 獲取文件輸入流
FileInputStream fileInputStream = getFileInputStream("666.docx");
dealDocx(fileInputStream, "副本.docx");
}
private static FileInputStream getFileInputStream(String name) throws FileNotFoundException {
String dir = poi3.class.getResource("").getPath() + name;
FileInputStream fileInputStream = new FileInputStream(dir);
return fileInputStream;
}
private static void dealDocx(InputStream inputStream, String newFileName) throws IOException {
// 創(chuàng)建輸出文件
File file = new File(poi3.class.getResource("").getPath() + newFileName);
// 獲取文件輸出流
FileOutputStream fileOutputStream = new FileOutputStream(file);
// 創(chuàng)建操作word的對象
XWPFDocument wordInput = new XWPFDocument(inputStream);
XWPFDocument wordOutput = new XWPFDocument();
// 獲取所有段落
List<XWPFParagraph> xwpfParagraphs = wordInput.getParagraphs();
// 迭代每一個段落
for (XWPFParagraph xwpfParagraph : xwpfParagraphs) {
// 原文檔有多少個段落 我就創(chuàng)建多少個
XWPFParagraph wordOutputParagraph = wordOutput.createParagraph();
// 獲取當(dāng)前段落的所有run
List<XWPFRun> runs = xwpfParagraph.getRuns();
for (XWPFRun run : runs) {
XWPFRun wordOutputParagraphRun = wordOutputParagraph.createRun();
// 賦值
//wordOutputParagraphRun.setText("哈哈哈哈~我修改過了");
// 添加回車 硬回車
//wordOutputParagraphRun.addCarriageReturn();
//wordOutputParagraphRun.addBreak(); // 軟回車
wordOutputParagraphRun.setText(run.getText(run.getCharacterSpacing()));
}
}
// 獲取所有表格
List<XWPFTable> xwpfTables = wordInput.getTables();
for (XWPFTable xwpfTable : xwpfTables) {
XWPFTable wordOutputTable = wordOutput.createTable();
// 獲取一個表格中的所有行
List<XWPFTableRow> xwpfTableRows = xwpfTable.getRows();
System.out.println("xwpfTableRows個數(shù)"+xwpfTableRows.size());
for (XWPFTableRow xwpfTableRow : xwpfTableRows) {
XWPFTableRow wordOutputTableRow = wordOutputTable.createRow();
// 獲取一行的所有列
List<XWPFTableCell> xwpfTableCell = xwpfTableRow.getTableCells();
System.out.println("xwpfTableCell個數(shù)"+xwpfTableCell.size());
int index = 0;
for (XWPFTableCell tableCell : xwpfTableCell) {
index++;
XWPFTableCell wordOutputTableRowCell = wordOutputTableRow.createCell();
// 獲取單個列
//wordOutputTableRowCell.setText("哈哈哈哈~我修改過了");
System.out.println(tableCell.getText());
wordOutputTableRowCell.setText(tableCell.getText());
System.out.println("index:"+index);
}
wordOutputTable.removeRow(0);
}
//wordOutputTable.removeBorders(); 虛線邊框
}
CTDocument1 document = wordInput.getDocument();
System.out.println();
wordOutput.write(fileOutputStream);
wordInput.close();
wordOutput.close();
inputStream.close();
fileOutputStream.close();
}
}總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot的服務(wù)注冊與發(fā)現(xiàn)示例
本篇文章主要介紹了SpringBoot的服務(wù)注冊與發(fā)現(xiàn)示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
Java在Word中插入上標(biāo)和下標(biāo)的實現(xiàn)方法
在某些情況下,你可能需要在Microsoft?Word中插入上標(biāo)和下標(biāo)。例如,當(dāng)你正在創(chuàng)建一個涉及科學(xué)公式的學(xué)術(shù)文件時,在這篇文章中,你將學(xué)習(xí)如何使用Spire.Doc?for?Java庫在Word文檔中插入上標(biāo)和下標(biāo),需要的朋友可以參考下2022-10-10
使用springboot單元測試對weblistener的加載測試
這篇文章主要介紹了使用springboot單元測試對weblistener的加載測試,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10

