java?poi之XWPFDocument如何讀取word內(nèi)容并創(chuàng)建新的word
Poi的Word文檔結(jié)構(gòu)介紹
1、poi之word文檔結(jié)構(gòu)介紹之正文段落
一個(gè)文檔包含多個(gè)段落,一個(gè)段落包含多個(gè)Runs,一個(gè)Runs包含多個(gè)Run,Run是文檔的最小單元
- 獲取所有段落:List paragraphs = word.getParagraphs();
- 獲取一個(gè)段落中的所有Runs:List xwpfRuns = xwpfParagraph.getRuns();
- 獲取一個(gè)Runs中的一個(gè)Run:XWPFRun run = xwpfRuns.get(index);
2、poi之word文檔結(jié)構(gòu)介紹之正文表格
一個(gè)文檔包含多個(gè)表格,一個(gè)表格包含多行,一行包含多列(格),每一格的內(nèi)容相當(dāng)于一個(gè)完整的文檔
- 獲取所有表格:List xwpfTables = doc.getTables();
- 獲取一個(gè)表格中的所有行:List xwpfTableRows = xwpfTable.getRows();
- 獲取一行中的所有列:List xwpfTableCells = xwpfTableRow.getTableCells();
- 獲取一格里的內(nèi)容:List paragraphs = xwpfTableCell.getParagraphs();
之后和正文段落一樣
注:
- 表格的一格相當(dāng)于一個(gè)完整的docx文檔,只是沒有頁眉和頁腳。里面可以有表格,使用xwpfTableCell.getTables()獲取,and so on
- 在poi文檔中段落和表格是完全分開的,如果在兩個(gè)段落中有一個(gè)表格,在poi中是沒辦法確定表格在段落中間的。(當(dāng)然除非你本來知道了,這句是廢話)。只有文檔的格式固定,才能正確的得到文檔的結(jié)構(gòu)
3、poi之word文檔結(jié)構(gòu)介紹之頁眉
一個(gè)文檔可以有多個(gè)頁眉(不知道怎么會有多個(gè)頁眉。。。),頁眉里面可以包含段落和表格
- 獲取文檔的頁眉:List headerList = doc.getHeaderList();
- 獲取頁眉里的所有段落:List paras = header.getParagraphs();
- 獲取頁眉里的所有表格:List tables = header.getTables();
之后就一樣了
4、poi之word文檔結(jié)構(gòu)介紹之頁腳
頁腳和頁眉基本類似,可以獲取表示頁數(shù)的角標(biāo)
IBodyElement
-------------------迭代器(段落和表格)XWPFComment
-------------------評論(個(gè)人理解應(yīng)該是批注)XWPFSDT
XWPFFooter
-------------------頁腳XWPFFootnotes
-------------------腳注XWPFHeader
-------------------頁眉XWPFHyperlink
-------------------超鏈接XWPFNumbering
-------------------編號XWPFParagraph
-------------------段落XWPFPictureData
-------------------圖片XWPFStyles
-------------------樣式(設(shè)置多級標(biāo)題的時(shí)候用)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項(xiàng)目
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(); // 迭代每一個(gè)段落 for (XWPFParagraph xwpfParagraph : xwpfParagraphs) { // 原文檔有多少個(gè)段落 我就創(chuàng)建多少個(gè) 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(); // 獲取一個(gè)表格中的所有行 List<XWPFTableRow> xwpfTableRows = xwpfTable.getRows(); System.out.println("xwpfTableRows個(gè)數(shù)"+xwpfTableRows.size()); for (XWPFTableRow xwpfTableRow : xwpfTableRows) { XWPFTableRow wordOutputTableRow = wordOutputTable.createRow(); // 獲取一行的所有列 List<XWPFTableCell> xwpfTableCell = xwpfTableRow.getTableCells(); System.out.println("xwpfTableCell個(gè)數(shù)"+xwpfTableCell.size()); int index = 0; for (XWPFTableCell tableCell : xwpfTableCell) { index++; XWPFTableCell wordOutputTableRowCell = wordOutputTableRow.createCell(); // 獲取單個(gè)列 //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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JAVA中while循環(huán)的使用與注意事項(xiàng)
這篇文章主要介紹了while循環(huán)在編程中的應(yīng)用,包括其基本結(jié)構(gòu)、語句示例、適用場景以及注意事項(xiàng),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01java接口用戶上下文的設(shè)計(jì)與實(shí)現(xiàn)
這篇文章主要為大家介紹了接口用戶上下文的設(shè)計(jì)與實(shí)現(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11springboot整合freemarker代碼自動(dòng)生成器
最近做了一個(gè)工具,可以實(shí)現(xiàn)代碼自動(dòng)生成,今天整理出來分享給大家,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05SpringBoot的服務(wù)注冊與發(fā)現(xiàn)示例
本篇文章主要介紹了SpringBoot的服務(wù)注冊與發(fā)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Java在Word中插入上標(biāo)和下標(biāo)的實(shí)現(xiàn)方法
在某些情況下,你可能需要在Microsoft?Word中插入上標(biāo)和下標(biāo)。例如,當(dāng)你正在創(chuàng)建一個(gè)涉及科學(xué)公式的學(xué)術(shù)文件時(shí),在這篇文章中,你將學(xué)習(xí)如何使用Spire.Doc?for?Java庫在Word文檔中插入上標(biāo)和下標(biāo),需要的朋友可以參考下2022-10-10使用springboot單元測試對weblistener的加載測試
這篇文章主要介紹了使用springboot單元測試對weblistener的加載測試,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10