简体   繁体   English

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

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

I'm working on a JAVA web application, I need to merge multiple docx files into one docx file using JAVA.我正在开发一个 JAVA web 应用程序,我需要使用 JAVA 将多个 docx 文件合并到一个 docx 文件中。

the method takes a list of File as parameter, the output is a single docx file that contains all data concatenated from Files in input.该方法将文件列表作为参数,output 是一个单独的 docx 文件,其中包含从输入文件中连接的所有数据。

I tried this code but it did not work for me:我尝试了这段代码,但它对我不起作用:

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);
    }
}

This method did not acheive my purpose.这种方法没有达到我的目的。 Any other suggestions?还有其他建议吗?

The error is clearly because of this line: return (File) source.错误显然是因为这一行:return (File) source。 source is a CTBody type and you cast it to File while they are not related. source 是 CTBody 类型,当它们不相关时将其转换为 File。

I have solved the issue of merging a list of docx files in one file, this is my code:我已经解决了将 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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM