Java實現(xiàn)合并兩個word文檔內(nèi)容
要在Java中實現(xiàn)將兩個Word文檔的內(nèi)容合并,可以使用Apache POI庫來操作Word文檔。
下面2個word合并包括以下內(nèi)容的合并和處理:
1、段落、標題合并以及樣式處理
2、表格合并以及樣式處理
3、單元個內(nèi)容樣式處理
4、帶word模板占位符內(nèi)容處理
步驟:
使用Apache POI讀取兩個Word文檔。
將第二個文檔的內(nèi)容追加到第一個文檔的末尾。
保存合并后的Word文檔。
依賴配置:
首先,需要在項目中添加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>
代碼實現(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 { /** * 合并兩個Word文檔,將第二個文件合并到第一個文件中 * * @param firstFile 第一個文件 * @param secondStream 上傳的文件 * @param outputFile 輸出文件 * @throws IOException IOException */ public static void mergeWordDocuments(String firstFile, InputStream secondStream, String outputFile) throws IOException { // 使用 try-with-resources 自動關(guān)閉資源 try (FileInputStream fis1 = new FileInputStream(firstFile); FileOutputStream out = new FileOutputStream(outputFile)) { // 加載第一個文檔 XWPFDocument doc1 = new XWPFDocument(fis1); // 加載第二個文檔 XWPFDocument doc2 = new XWPFDocument(secondStream); // 合并文檔 appendDocument(doc1, doc2); // 保存合并后的文檔 doc1.write(out); } } /** * 合并兩個Word文檔,將第二個文件合并到第一個文件中 * * @param firstFile 第一個文件 * @param secondFile 第二個文件 * @param outputFile 輸出文件 * @throws IOException IOException */ public static void mergeWordDocuments(String firstFile, String secondFile, String outputFile) throws IOException { // 使用 try-with-resources 自動關(guān)閉資源 try (FileInputStream fis1 = new FileInputStream(firstFile); FileInputStream fis2 = new FileInputStream(secondFile); FileOutputStream out = new FileOutputStream(outputFile)) { // 加載第一個文檔 XWPFDocument doc1 = new XWPFDocument(fis1); // 加載第二個文檔 XWPFDocument doc2 = new XWPFDocument(fis2); // 合并文檔 appendDocument(doc1, doc2); // 保存合并后的文檔 doc1.write(out); } } public static void appendDocument(XWPFDocument doc1, XWPFDocument doc2) { // 獲取第二個文檔的所有部分,保持順序合并 for (IBodyElement element : doc2.getBodyElements()) { // 根據(jù)元素的類型進行不同的處理 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) { // 只復制段落的布局屬性(例如對齊、縮進、行距等),但不包括段落樣式(避免改變標題級別) target.getCTP().setPPr(source.getCTP().getPPr()); // 復制對齊方式、縮進、行間距等樣式 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()); // 復制段落中的每個run for (XWPFRun run : source.getRuns()) { XWPFRun newRun = target.createRun(); newRun.setText(run.text()); // 復制格式 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()); } } // 復制源表格到目標表格,包括其行和單元格的樣式和內(nèi)容 private static void copyTable(XWPFTable source, XWPFTable target) { // 刪除目標表格中的默認創(chuàng)建的第一行,確保從空狀態(tài)開始復制 target.removeRow(0); // 遍歷源表格的每一行 for (XWPFTableRow sourceRow : source.getRows()) { // 在目標表格中創(chuàng)建新行 XWPFTableRow newTableRow = target.createRow(); // 復制源行的樣式和內(nèi)容到目標新行 copyTableRow(sourceRow, newTableRow); } } // 將源表格行的樣式和內(nèi)容復制到目標表格行 private static void copyTableRow(XWPFTableRow source, XWPFTableRow target) { // 復制行的樣式屬性 target.getCtRow().setTrPr(source.getCtRow().getTrPr()); // 遍歷源行的每一個單元格 for (int i = 0; i < source.getTableCells().size(); i++) { XWPFTableCell sourceCell = source.getCell(i); XWPFTableCell targetCell; // 如果目標行的單元格還不夠,則創(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")) { // 復制單元格的樣式屬性 targetCell.getCTTc().setTcPr(sourceCell.getCTTc().getTcPr()); targetCell.setText(sourceCell.getText()); } else { // 復制單元格的樣式和內(nèi)容 copyTableCell(sourceCell, targetCell); } } } // 將源單元格的樣式和內(nèi)容復制到目標單元格 private static void copyTableCell(XWPFTableCell source, XWPFTableCell target) { // 復制單元格的樣式屬性 target.getCTTc().setTcPr(source.getCTTc().getTcPr()); // 清除目標單元格的段落 target.getParagraphs().clear(); // 復制每個段落 for (XWPFParagraph sourceParagraph : source.getParagraphs()) { XWPFParagraph targetParagraph = target.addParagraph(); copyParagraph(sourceParagraph, targetParagraph); } } }
說明:
XWPFDocument:用于讀取和寫入Word文檔(.docx格式)。我們首先通過FileInputStream讀取Word文檔。
mergeDocuments:該方法將第二個Word文檔的所有段落復制到第一個文檔中。我們獲取第二個文檔的所有段落,并將其逐個添加到第一個文檔。
XWPFParagraph:表示W(wǎng)ord文檔中的一個段落。每個段落包含多個“運行”(XWPFRun),每個“運行”代表文檔中的一部分文本。
XWPFRun:表示段落中的一段文本,可以設(shè)置文本的樣式(如字體、大小、加粗、斜體等)。
注意:
合并邏輯:本例中只是簡單地將第二個文檔的所有段落復制到第一個文檔。你可以根據(jù)需求修改合并的細節(jié)(如按頁、按段落或按表格等方式進行合并)。
合并樣式:此示例中也將復制了文本的樣式(如字體、大小、加粗等)。如果需要更復雜的樣式保留,可以根據(jù)具體的需求進行調(diào)整。
處理表格、圖片等:如果文檔中包含表格或圖片,你可能需要額外的邏輯來處理這些內(nèi)容。
結(jié)果:
運行以上代碼后,你將得到一個新的Word文檔,內(nèi)容包含了兩個原始文檔的所有內(nèi)容。
到此這篇關(guān)于Java實現(xiàn)合并兩個word文檔內(nèi)容的文章就介紹到這了,更多相關(guān)Java合并word內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud微服務(wù)熔斷器Hystrix使用詳解
這篇文章主要介紹了Spring Cloud Hyxtrix的基本使用,它是Spring Cloud中集成的一個組件,在整個生態(tài)中主要為我們提供服務(wù)隔離,服務(wù)熔斷,服務(wù)降級功能,本文給大家介紹的非常詳細,需要的朋友可以參考下2022-07-07Java阻塞隊列BlockingQueue基礎(chǔ)與使用
本文詳細介紹了BlockingQueue家庭中的所有成員,包括他們各自的功能以及常見使用場景,通過實例代碼介紹了Java 阻塞隊列BlockingQueue的相關(guān)知識,需要的朋友可以參考下2023-01-01Java8中LocalDateTime與時間戳timestamp的互相轉(zhuǎn)換
這篇文章主要給大家介紹了關(guān)于Java8中LocalDateTime與時間戳timestamp的互相轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03Spring security 如何開放 Swagger 訪問權(quán)限
這篇文章主要介紹了Spring security 如何開放 Swagger 訪問權(quán)限操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09