Java實(shí)現(xiàn)合并兩個(gè)word文檔內(nèi)容
要在Java中實(shí)現(xiàn)將兩個(gè)Word文檔的內(nèi)容合并,可以使用Apache POI庫(kù)來(lái)操作Word文檔。
下面2個(gè)word合并包括以下內(nèi)容的合并和處理:
1、段落、標(biāo)題合并以及樣式處理
2、表格合并以及樣式處理
3、單元個(gè)內(nèi)容樣式處理
4、帶word模板占位符內(nèi)容處理
步驟:
使用Apache POI讀取兩個(gè)Word文檔。
將第二個(gè)文檔的內(nèi)容追加到第一個(gè)文檔的末尾。
保存合并后的Word文檔。
依賴配置:
首先,需要在項(xiàng)目中添加Apache POI相關(guān)的依賴。如果使用Maven,可以在pom.xml文件中加入以下依賴:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.21</version> </dependency> <!-- Apache POI for Excel --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> <!-- Apache POI for Word --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>4.1.2</version> </dependency> <!-- Apache POI dependencies --> <dependency> <groupId>org.apache.xmlbeans</groupId> <artifactId>xmlbeans</artifactId> <version>3.1.0</version> </dependency>
代碼實(shí)現(xiàn):
package com.meritdata.ddc.common.util; import org.apache.poi.xwpf.usermodel.*; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; /** * @author dongxiajun * @since 2024-11-14 15:15 */ public class WordMerger { /** * 合并兩個(gè)Word文檔,將第二個(gè)文件合并到第一個(gè)文件中 * * @param firstFile 第一個(gè)文件 * @param secondStream 上傳的文件 * @param outputFile 輸出文件 * @throws IOException IOException */ public static void mergeWordDocuments(String firstFile, InputStream secondStream, String outputFile) throws IOException { // 使用 try-with-resources 自動(dòng)關(guān)閉資源 try (FileInputStream fis1 = new FileInputStream(firstFile); FileOutputStream out = new FileOutputStream(outputFile)) { // 加載第一個(gè)文檔 XWPFDocument doc1 = new XWPFDocument(fis1); // 加載第二個(gè)文檔 XWPFDocument doc2 = new XWPFDocument(secondStream); // 合并文檔 appendDocument(doc1, doc2); // 保存合并后的文檔 doc1.write(out); } } /** * 合并兩個(gè)Word文檔,將第二個(gè)文件合并到第一個(gè)文件中 * * @param firstFile 第一個(gè)文件 * @param secondFile 第二個(gè)文件 * @param outputFile 輸出文件 * @throws IOException IOException */ public static void mergeWordDocuments(String firstFile, String secondFile, String outputFile) throws IOException { // 使用 try-with-resources 自動(dòng)關(guān)閉資源 try (FileInputStream fis1 = new FileInputStream(firstFile); FileInputStream fis2 = new FileInputStream(secondFile); FileOutputStream out = new FileOutputStream(outputFile)) { // 加載第一個(gè)文檔 XWPFDocument doc1 = new XWPFDocument(fis1); // 加載第二個(gè)文檔 XWPFDocument doc2 = new XWPFDocument(fis2); // 合并文檔 appendDocument(doc1, doc2); // 保存合并后的文檔 doc1.write(out); } } public static void appendDocument(XWPFDocument doc1, XWPFDocument doc2) { // 獲取第二個(gè)文檔的所有部分,保持順序合并 for (IBodyElement element : doc2.getBodyElements()) { // 根據(jù)元素的類型進(jìn)行不同的處理 if (element instanceof XWPFParagraph) { XWPFParagraph paragraph = (XWPFParagraph) element; XWPFParagraph newParagraph = doc1.createParagraph(); copyParagraph(paragraph, newParagraph); } else if (element instanceof XWPFTable) { XWPFTable table = (XWPFTable) element; XWPFTable newTable = doc1.createTable(); copyTable(table, newTable); } } } private static void copyParagraph(XWPFParagraph source, XWPFParagraph target) { // 只復(fù)制段落的布局屬性(例如對(duì)齊、縮進(jìn)、行距等),但不包括段落樣式(避免改變標(biāo)題級(jí)別) target.getCTP().setPPr(source.getCTP().getPPr()); // 復(fù)制對(duì)齊方式、縮進(jìn)、行間距等樣式 target.setAlignment(source.getAlignment()); target.setVerticalAlignment(source.getVerticalAlignment()); target.setIndentationLeft(source.getIndentationLeft()); target.setIndentationRight(source.getIndentationRight()); target.setIndentationFirstLine(source.getIndentationFirstLine()); target.setSpacingBefore(source.getSpacingBefore()); target.setSpacingAfter(source.getSpacingAfter()); target.setSpacingBetween(source.getSpacingBetween()); // 復(fù)制段落中的每個(gè)run for (XWPFRun run : source.getRuns()) { XWPFRun newRun = target.createRun(); newRun.setText(run.text()); // 復(fù)制格式 newRun.setBold(run.isBold()); newRun.setItalic(run.isItalic()); newRun.setUnderline(run.getUnderline()); newRun.setColor(run.getColor()); newRun.setFontSize(run.getFontSize()); newRun.setFontFamily(run.getFontFamily()); } if (source.getStyle() != null) { target.setStyle(source.getStyle()); } } // 復(fù)制源表格到目標(biāo)表格,包括其行和單元格的樣式和內(nèi)容 private static void copyTable(XWPFTable source, XWPFTable target) { // 刪除目標(biāo)表格中的默認(rèn)創(chuàng)建的第一行,確保從空狀態(tài)開始復(fù)制 target.removeRow(0); // 遍歷源表格的每一行 for (XWPFTableRow sourceRow : source.getRows()) { // 在目標(biāo)表格中創(chuàng)建新行 XWPFTableRow newTableRow = target.createRow(); // 復(fù)制源行的樣式和內(nèi)容到目標(biāo)新行 copyTableRow(sourceRow, newTableRow); } } // 將源表格行的樣式和內(nèi)容復(fù)制到目標(biāo)表格行 private static void copyTableRow(XWPFTableRow source, XWPFTableRow target) { // 復(fù)制行的樣式屬性 target.getCtRow().setTrPr(source.getCtRow().getTrPr()); // 遍歷源行的每一個(gè)單元格 for (int i = 0; i < source.getTableCells().size(); i++) { XWPFTableCell sourceCell = source.getCell(i); XWPFTableCell targetCell; // 如果目標(biāo)行的單元格還不夠,則創(chuàng)建新單元格 if (i < target.getTableCells().size()) { targetCell = target.getCell(i); } else { targetCell = target.addNewTableCell(); } String text = source.getCell(0).getText(); if (text.contains("{{$fe")) { // 復(fù)制單元格的樣式屬性 targetCell.getCTTc().setTcPr(sourceCell.getCTTc().getTcPr()); targetCell.setText(sourceCell.getText()); } else { // 復(fù)制單元格的樣式和內(nèi)容 copyTableCell(sourceCell, targetCell); } } } // 將源單元格的樣式和內(nèi)容復(fù)制到目標(biāo)單元格 private static void copyTableCell(XWPFTableCell source, XWPFTableCell target) { // 復(fù)制單元格的樣式屬性 target.getCTTc().setTcPr(source.getCTTc().getTcPr()); // 清除目標(biāo)單元格的段落 target.getParagraphs().clear(); // 復(fù)制每個(gè)段落 for (XWPFParagraph sourceParagraph : source.getParagraphs()) { XWPFParagraph targetParagraph = target.addParagraph(); copyParagraph(sourceParagraph, targetParagraph); } } }
說(shuō)明:
XWPFDocument:用于讀取和寫入Word文檔(.docx格式)。我們首先通過(guò)FileInputStream讀取Word文檔。
mergeDocuments:該方法將第二個(gè)Word文檔的所有段落復(fù)制到第一個(gè)文檔中。我們獲取第二個(gè)文檔的所有段落,并將其逐個(gè)添加到第一個(gè)文檔。
XWPFParagraph:表示W(wǎng)ord文檔中的一個(gè)段落。每個(gè)段落包含多個(gè)“運(yùn)行”(XWPFRun),每個(gè)“運(yùn)行”代表文檔中的一部分文本。
XWPFRun:表示段落中的一段文本,可以設(shè)置文本的樣式(如字體、大小、加粗、斜體等)。
注意:
合并邏輯:本例中只是簡(jiǎn)單地將第二個(gè)文檔的所有段落復(fù)制到第一個(gè)文檔。你可以根據(jù)需求修改合并的細(xì)節(jié)(如按頁(yè)、按段落或按表格等方式進(jìn)行合并)。
合并樣式:此示例中也將復(fù)制了文本的樣式(如字體、大小、加粗等)。如果需要更復(fù)雜的樣式保留,可以根據(jù)具體的需求進(jìn)行調(diào)整。
處理表格、圖片等:如果文檔中包含表格或圖片,你可能需要額外的邏輯來(lái)處理這些內(nèi)容。
結(jié)果:
運(yùn)行以上代碼后,你將得到一個(gè)新的Word文檔,內(nèi)容包含了兩個(gè)原始文檔的所有內(nèi)容。
到此這篇關(guān)于Java實(shí)現(xiàn)合并兩個(gè)word文檔內(nèi)容的文章就介紹到這了,更多相關(guān)Java合并word內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud微服務(wù)熔斷器Hystrix使用詳解
這篇文章主要介紹了Spring Cloud Hyxtrix的基本使用,它是Spring Cloud中集成的一個(gè)組件,在整個(gè)生態(tài)中主要為我們提供服務(wù)隔離,服務(wù)熔斷,服務(wù)降級(jí)功能,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07零基礎(chǔ)寫Java知乎爬蟲之先拿百度首頁(yè)練練手
本來(lái)打算這篇文章直接抓取知乎的,但是想想還是先來(lái)個(gè)簡(jiǎn)單的吧,初級(jí)文章適合初學(xué)者,高手們請(qǐng)直接略過(guò)2014-11-11Java阻塞隊(duì)列BlockingQueue基礎(chǔ)與使用
本文詳細(xì)介紹了BlockingQueue家庭中的所有成員,包括他們各自的功能以及常見使用場(chǎng)景,通過(guò)實(shí)例代碼介紹了Java 阻塞隊(duì)列BlockingQueue的相關(guān)知識(shí),需要的朋友可以參考下2023-01-01Java時(shí)間輪算法的實(shí)現(xiàn)代碼示例
本篇文章主要介紹了Java時(shí)間輪算法的實(shí)現(xiàn)代碼示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08Java8中LocalDateTime與時(shí)間戳timestamp的互相轉(zhuǎn)換
這篇文章主要給大家介紹了關(guān)于Java8中LocalDateTime與時(shí)間戳timestamp的互相轉(zhuǎn)換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03Spring security 如何開放 Swagger 訪問權(quán)限
這篇文章主要介紹了Spring security 如何開放 Swagger 訪問權(quán)限操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09