poi实现多个word文档的合并测试代码样例

提供一个代码样例实现使用poi工具包合并多个word文档:

工具类 MergeDocUtils

package com.yootii.bdy.util.test;

import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MergeDocUtils {
    public static void mergeDoc(List<File> fileList, File newFile) {
        try {
            System.out.println("正在合并文件:" + newFile);
            OutputStream dest = Files.newOutputStream(newFile.toPath());
            if(CollectionUtils.isEmpty(fileList)){
                System.out.println("待合并文件为空,合并文件失败:" + newFile.toString());
                return;
            }
            // 判断文件格式是不是以docx或doc结尾
            for (int i = 0; i < fileList.size(); i++) {
                if (!(fileList.get(i).getName().toLowerCase().endsWith(".doc")
                        || fileList.get(i).getName().toLowerCase().endsWith(".docx"))) {
                    System.out.println("文件格式错误,合并失败:" + newFile.toString());
                    break;
                }
            }
            // 打开第一个文件
//			Path path1 = Paths.get(fileList.get(0));
//			boolean exists = Files.exists(path1);
//			System.out.println("文件是否存在: " + exists);
            ArrayList<XWPFDocument> documentList = new ArrayList<XWPFDocument>();
            XWPFDocument doc = null;
            for (int i = 0; i < fileList.size(); i++) {
                FileInputStream in = new FileInputStream(fileList.get(i).getPath());
                OPCPackage open = OPCPackage.open(in);
                XWPFDocument document = new XWPFDocument(open);
                documentList.add(document);
            }
            for (int i = 0; i < documentList.size(); i++) {
                doc = documentList.get(0);
                // 加入分页
                if (i != documentList.size()-1){
                    documentList.get(i).createParagraph().createRun().addBreak(BreakType.PAGE);
                }
                // 从第二个文件开始追加合并
                if (i != 0) {
                    // 但是会出现在首行为空的情况
//                    documentList.get(i).createParagraph().setPageBreak(true);
                    appendBody(doc, documentList.get(i));
                }
            }
            // 输出合并之后的文件
            if (doc != null) {
                doc.write(dest);
            }
        }catch (Exception e){
            e.printStackTrace();
//            System.out.println("合并文件失败:" + newFile.toString());
        }
    }

    public static void appendBody(XWPFDocument src, XWPFDocument append) throws Exception {
        CTBody src1Body = src.getDocument().getBody();
        CTBody src2Body = append.getDocument().getBody();
        List<XWPFPictureData> allPictures = append.getAllPictures();
        // 记录图片合并前及合并后的ID
        Map<String, String> map = new HashMap();
        for (XWPFPictureData picture : allPictures) {
            String before = append.getRelationId(picture);
            // 将原文档中的图片加入到目标文档中
            String after = src.addPictureData(picture.getData(), Document.PICTURE_TYPE_PNG);
            map.put(before, after);
        }
        appendBodyDetail(src1Body, src2Body, map);
    }

    private static void appendBodyDetail(CTBody src, CTBody append, Map<String, String> map) throws Exception {
        XmlOptions optionsOuter = new XmlOptions();
        optionsOuter.setSaveOuter();
        String appendString = append.xmlText(optionsOuter);
        // 去掉追加word内容中的 w:sectPr 标签,确保合成的word中只有一个 w:sectPr 标签对
        // 避免合成的word文档打开之后会提示有些内容读不出来,导致文件损坏
        String rgex = "<[\\s]*?w:sectPr[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?w:sectPr[\\s]*?>";
        appendString = appendString.replaceAll(rgex, "");
        String srcString = src.xmlText();
//        String regex = regexText(srcString, "w:sectPr");
//        System.out.println(regex);
        String prefix = srcString.substring(0, srcString.indexOf(">") + 1);
        String mainPart = srcString.substring(srcString.indexOf(">") + 1, srcString.lastIndexOf("<"));
        String sufix = srcString.substring(srcString.lastIndexOf("<"));
        String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));

        if (map != null && !map.isEmpty()) {
            // 对xml字符串中图片ID进行替换
            for (Map.Entry<String, String> set : map.entrySet()) {
                addPart = addPart.replace(set.getKey(), set.getValue());
            }
        }
        // 将两个文档的xml内容进行拼接
        CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + sufix);
        src.set(makeBody);
    }
}

测试类 WordMergeTest

package com.yootii.bdy.util.test;

import java.io.File;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @BelongsProject: ip_service
 * @BelongsPackage: com.yootii.bdy.util.test
 * @Author: chuanwei.yang 42624
 * @CreateTime: 2024-08-12  11:47
 * @Description: TODO
 * @Version: 1.0
 */
public class WordMergeTest {
    public static void main(String[] args) throws Exception {
        // new一个list 模拟要合并的word对象集合
        List<File> docFileList = new ArrayList<>();
        docFileList.add(new File("D:\\ipdoc\\template\\POA\\2\\LD2021000106\\1723521822027\\所函(二审)LD2021000106.doc"));
        docFileList.add(new File("D:\\ipdoc\\template\\POA\\2\\LD2021000106\\1723521822027\\所函(一审)-第三人LD2021000115.doc"));
//        docFileList.add(new File("D:\\ipdoc\\template\\POA\\2\\LD2021000335\\1723430865470\\测试文件test.docx"));
//        docFileList.add(new File("D:\\ipdoc\\template\\POA\\2\\LD2021000335\\1723430865470\\测试文件test2.docx"));

        //合并之后doc存储路径 此处读的配置文件的存储路径 D:/pdfData/
        String docPath = "D:\\ipdoc\\template\\POA\\2\\LD2021000335\\1723430865470\\";
        //当前日期+UUID 作为文件名防止重复
        String fileName = LocalDate.now() + "_" + UUID.randomUUID().toString().replaceAll("-", "");
        //合并之后doc存储路径
        String mergeDocUrl = docPath+fileName+".docx";
        //转成file对象
        File mergeDocFile = new File(mergeDocUrl);
        //合并doc
        MergeDocUtils.mergeDoc(docFileList,mergeDocFile);
        System.out.println("合并word成功");
    }
}

把测试方法里的文件路径改一下,改成你自己要用的文件就可以,实现效果就是例如把文件A和B合并在一起,文件A有2.5页(也就是第三页没占满),文件B有1页,合并之后的文件有4页,会在每个独立文件之后插入一个分页符再进行新文件内容的追加,保证阅读的连贯性。

您可以使用Apache POI库来实现合并多个Word文档。以下是Java代码示例: ``` import java.io.*; import org.apache.poi.xwpf.usermodel.*; public class MergeWordDocs { public static void main(String[] args) throws Exception { //创建输出文档 XWPFDocument outputDoc = new XWPFDocument(); //获取多个合并的文档路径 String[] inputDocs = new String[]{"path1.docx", "path2.docx"}; //循环遍历待合并的文档 for (String inputDoc : inputDocs) { //读取待合并文档 XWPFDocument inputDocx = new XWPFDocument(new FileInputStream(inputDoc)); //获取待合并文档的段落 for (XWPFParagraph paragraph : inputDocx.getParagraphs()) { //将待合并文档的段落添加到输出文档 outputDoc.createParagraph().createRun().setText(paragraph.getText()); } //获取待合并文档的表格 for (XWPFTable table : inputDocx.getTables()) { //将待合并文档的表格添加到输出文档 outputDoc.createTable().addRow(table.getRow(0)); for (int i = 1; i < table.getRows().size(); i++) { outputDoc.getTables().get(outputDoc.getTables().size() - 1).addRow(table.getRow(i)); } } } //输出合并后的文档 FileOutputStream out = new FileOutputStream("merged.docx"); outputDoc.write(out); out.close(); outputDoc.close(); } } ``` 这段代码使用了XWPFDocument类来读取和创建Word文档,使用XWPFParagraph类访问文档段落,使用XWPFTable类访问文档表格。循环读取待合并文档的段落和表格,将其添加到输出文档中。最后使用FileOutputStream类将输出文档写入新的Word文档文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

靠谱杨

感谢您的认可和鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值