如何在 JAVA 中将多个 .docx 文件合并为一个文件?

How to merge multiple .docx files into one file in JAVA?

我正在开发一个 JAVA 网络应用程序,我需要使用 JAVA.

将多个 docx 文件合并到一个 docx 文件中

该方法将文件列表作为参数,输出是单个 docx 文件,其中包含从输入文件连接的所有数据。

我试过这段代码,但它对我不起作用:

public File mergeInOneFile(List<File> files) throws IOException, InvalidFormatException, XmlException {
        List<CTBody> sourceBody = new ArrayList<>();
        for (File file : files) {
            OPCPackage srcFile = OPCPackage.open(file);
            XWPFDocument srcDocument = new XWPFDocument(srcFile);
            CTBody srcBody = srcDocument.getDocument().getBody();
            sourceBody.add(srcBody);
        }
        CTBody source = sourceBody.get(0);
        sourceBody.remove(0);
        while (sourceBody.size() != 0){
            appendBody(source, sourceBody.get(0));
            sourceBody.remove(0);
        }
        return (File) source;
    }

private static void appendBody(CTBody src, CTBody append) throws XmlException {
        XmlOptions optionsOuter = new XmlOptions();
        optionsOuter.setSaveOuter();
        String appendString = append.xmlText(optionsOuter);
        String srcString = src.xmlText();
        String prefix = srcString.substring(0,srcString.indexOf(">")+1);
        String mainPart = srcString.substring(srcString.indexOf(">")+1,srcString.lastIndexOf("<"));
        String suffix = srcString.substring( srcString.lastIndexOf("<") );
        String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));
        CTBody makeBody = CTBody.Factory.parse(prefix+mainPart+addPart+suffix);
        src.set(makeBody);
    }
}

这个方法没有达到我的目的。还有其他建议吗?

错误显然是因为这一行:return (File) source。 source 是 CTBody 类型,当它们不相关时你将它转换为 File。

我已经解决了将一系列 docx 文件合并到一个文件中的问题,这是我的代码:

public File merge(final List<File> files) throws IoAppException, OtherAppException {
        Path outPath = this.fileStorageService.getPath()
                .resolve(UUID.randomUUID().toString() + Directory.DOCX_EXTENSION);
        File outFile = outPath.toFile();
        try (OutputStream os = new FileOutputStream(outFile);
                InputStream is = new FileInputStream(files.get(0));
                XWPFDocument doc = new XWPFDocument(is);) {


            CTBody ctBody = doc.getDocument().getBody();
            for (int i = 1; i < files.size(); i++) {
                try (InputStream isI = new FileInputStream(files.get(i)); XWPFDocument docI = new XWPFDocument(isI);) {

                    CTBody ctBodyI = docI.getDocument().getBody();
                    appendBody(ctBody, ctBodyI);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new OtherAppException("", e);
                }
            }
            doc.write(os);
            return outFile;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new IoAppException("", e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new IoAppException("", e);
        }
    }
private static void appendBody(CTBody src, CTBody append) throws XmlException {
        XmlOptions optionsOuter = new XmlOptions();
        optionsOuter.setSaveOuter();
        String appendString = append.xmlText(optionsOuter);
        String srcString = src.xmlText();
        String prefix = srcString.substring(0, srcString.indexOf(">") + 1);
        String mainPart = srcString.substring(srcString.indexOf(">") + 1, srcString.lastIndexOf("<"));
        String suffix = srcString.substring(srcString.lastIndexOf("<"));
        String addPart = appendString.substring(appendString.indexOf(">") + 1, appendString.lastIndexOf("<"));
        CTBody makeBody;
        try {
            makeBody = CTBody.Factory.parse(prefix + mainPart + addPart + suffix);
        } catch (XmlException e) {
            e.printStackTrace();
            throw new XmlException("", e);
        }
        src.set(makeBody);
    }