2016-09-23 98 views
0

我想将多个docx文件合并为一个项目。但是我做了一些研究,并且没有办法找到可以在Java或Powershell中处理它的代码。这些是最接近我想要的东西,但它不支持多个文件。Java/Powershell:将大量docx文档合并为一个,并保留所有文档的格式

package wordv2; 

import java.io.*; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 

public class Wordv2 { 

    public static void main(String[] args) { 
     FileInputStream instream = null; 

     FileOutputStream outstream = null; 

     try { 
      File infile = new File("C:\\jeux\\test.docx"); 

      File outfile = new File("C:\\jeux\\MyOutputFile.docx"); 

      instream = new FileInputStream(infile); 

      outstream = new FileOutputStream(outfile); 

      byte[] buffer = new byte[1024]; 

      int length; 

      /*copying the contents from input stream to 
      * output stream using read and write methods 
      */ 
      while ((length = instream.read(buffer)) > 0) { 
       outstream.write(buffer, 0, length); 
      } 

      //Closing the input/output file streams 
      instream.close(); 
      outstream.close(); 

      System.out.println("File copied successfully!!"); 

     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
     } 
    } 
} 

你有什么想法,使其能够处理我的请求,或者你知道的任何其他代码,能做到吗?

回答

0

我建议使用Apache POI它是一个很好的Microsoft API文档。

至于Java部分,你可能想要一个新的文件合并所有现有的文件。如果您的文件全部位于磁盘上的同一目录中,则可以遍历目录中的所有文件,并继续写入您在开始时创建的新文件。我推荐看https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#walkFileTree-java.nio.file.Path-java.util.Set-int-java.nio.file.FileVisitor-

//create new word document 
Files.walk(path).forEach(if a docx open it, write into a previously created and close) 
//save new document 
+0

对不起,请问你有没有exempel?我是一个非常漂亮的新程序员,我尽我最大的努力,但并不是总能找到其他人试图解释的东西,并且确实格式不对吗? – Elaxe

+0

@Elaxe https://www.tutorialspoint.com/apache_poi_word/apache_poi_word_quick_guide.htm – daddycool

+0

@Elaxe我也建议看看http://stackoverflow.com/questions/25130419/how-to-copy-some-content-in-一DOCX到使用-POI-而不-失去感到另一个-DOCX- – daddycool

相关问题