Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JAVA中将多个.docx文件合并到一个文件中?_Java_Apache Poi_Xwpf - Fatal编程技术网

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

如何在JAVA中将多个.docx文件合并到一个文件中?,java,apache-poi,xwpf,Java,Apache Poi,Xwpf,我正在开发一个JAVA web应用程序,我需要使用JAVA将多个docx文件合并到一个docx文件中 该方法以文件列表为参数,输出为单个docx文件,其中包含从输入中的文件连接而来的所有数据 我尝试了此代码,但对我无效: public File mergeInOneFile(List<File> files) throws IOException, InvalidFormatException, XmlException { List<CTBody> so

我正在开发一个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);
    }
}
公共文件mergeInOneFile(列表文件)引发IOException、InvalidFormatException、XmlException{
List sourceBody=new ArrayList();
用于(文件:文件){
OPCPackage srcFile=OPCPackage.open(文件);
XWPFDocument srcDocument=新的XWPFDocument(srcFile);
CTBody srcBody=srcDocument.getDocument().getBody();
添加(srcBody);
}
CTBody source=sourceBody.get(0);
sourceBody.remove(0);
while(sourceBody.size()!=0){
appendBody(source,sourceBody.get(0));
sourceBody.remove(0);
}
返回(文件)源;
}
私有静态void appendBody(CTBody src,CTBody append)引发XmlException{
XmlOptions options外部=新的XmlOptions();
optionsOuter.setSaveOuter();
字符串appendString=append.xmlText(选项外部);
String srcString=src.xmlText();
字符串前缀=srcString.substring(0,srcString.indexOf(“>”)+1);

String mainPart=srcString.substring(srcString.indexOf(“>”)+1,srcString.lastIndexOf(“错误显然是因为这一行:返回(文件)源。
source是CTBody类型,您可以在它们不相关时将其强制转换为文件。

错误显然是因为这一行:return(File)source。
source是CTBody类型,您可以在它们不相关时将其强制转换为文件。

我已经解决了将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);
        }
    }
公共文件合并(最终列表文件)抛出IoAppException、OtherAppException{
Path outPath=this.fileStorageService.getPath()
.resolve(UUID.randomuid().toString()+目录.DOCX_扩展名);
File outFile=outPath.toFile();
try(OutputStream os=newfileoutputstream(outFile);
InputStream is=新文件InputStream(files.get(0));
XWPFDocument doc=新XWPFDocument(is);){
CTBody CTBody=doc.getDocument().getBody();
对于(int i=1;i
private static void appendBody(CTBody src,CTBody append)抛出XmlException{
XmlOptions options外部=新的XmlOptions();
optionsOuter.setSaveOuter();
字符串appendString=append.xmlText(选项外部);
String srcString=src.xmlText();
字符串前缀=srcString.substring(0,srcString.indexOf(“>”)+1);

String mainPart=srcString.substring(srcString.indexOf(“>”)+1,srcString.lastinexof(“我已经解决了将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);
        }
    }
公共文件合并(最终列表文件)抛出IoAppException、OtherAppException{
Path outPath=this.fileStorageService.getPath()
.resolve(UUID.randomuid().toString()+目录.DOCX_扩展名);
File outFile=outPath.toFile();
try(OutputStream os=newfileoutputstream(outFile);
InputStream is=新文件InputStream(files.get(0));
XWPFDocument doc=新XWPFDocument(is);){
CTBody CTBody=doc.getDocument().getBody();
对于(int i=1;i
private static void appendBody(CTBody src,CTBody append)抛出XmlException{
XmlOptions options外部=新的XmlOptions();
optionsOuter.setSaveOuter();
字符串appendString=append.xmlText(选项外部);
String srcString=src.xmlText();
字符串前缀=srcString.substring(0,srcString.indexOf(“>”)+1);

String mainPart=srcString.substring(srcString.indexOf(“>”)+1,srcString.lastIndexOf(“是的,我知道,我只是问是否有其他方法将docx列表合并到一个docx中。我尝试了上面的代码,但由于强制转换,它不起作用。是的,我知道,我只是问是否有其他方法将docx列表合并到一个docx中。我尝试了上面的代码,但由于强制转换,它不起作用。