【问题标题】:How to merge multiple .docx files into one file in JAVA?如何在 JAVA 中将多个 .docx 文件合并为一个文件?
【发布时间】:2020-02-23 19:25:59
【问题描述】:

我正在开发一个 JAVA Web 应用程序,我需要使用 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);
    }
}

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

【问题讨论】:

    标签: java apache-poi xwpf


    【解决方案1】:

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

    【讨论】:

    • 是的,我知道,我在问是否有另一种方法可以将 docx 列表合并到一个 docx 中。我尝试了上面的代码,但由于强制转换,它无法工作。
    【解决方案2】:

    我已经解决了将 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);
        }
    

    【讨论】:

      猜你喜欢
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多