2016-09-29 102 views
4

我读了一个word文档并想用Java写入另一个word文件。我希望读取文档中内容的样式(字体,粗体,斜体,标题等)被写入,因为它是创建的新文档。 我能够复制内容,但不能格式化。用JAVA写入一个word文件

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import org.apache.poi.xwpf.usermodel.XWPFDocument; 
import org.apache.poi.xwpf.extractor.XWPFWordExtractor; 
import org.apache.poi.xwpf.usermodel.XWPFParagraph; 
import org.apache.poi.xwpf.usermodel.XWPFRun; 
import java.util.List; 

public class ReadFile 
{ 
    public static void main(String[] args)throws Exception 
    { 

     XWPFDocument docx = new XWPFDocument(new FileInputStream("d:\\Profiles\\mehjain\\Desktop\\Test1.docx")); 
     List<XWPFParagraph> paragraphList = docx.getParagraphs(); 

     XWPFDocument document= new XWPFDocument(); 
     FileOutputStream out = new FileOutputStream(new File("d:\\Profiles\\mehjain\\Desktop\\Test2.docx")); 
     XWPFParagraph n = document.createParagraph(); 
     XWPFRun run=n.createRun(); 

     for (XWPFParagraph paragraph: paragraphList) 
     { 
      run.setText(paragraph.getText());    
      run.addCarriageReturn(); 
     } 
     document.write(out); 
     document.close(); 
     out.close(); 
     System.out.println("Test2.docx written successfully"); 
    } 
} 

我得到了一个答案,复制相同格式的文本,但我无法复制数字。 我执行这个代码:

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import org.apache.poi.xwpf.usermodel.IBody; 
import org.apache.poi.xwpf.usermodel.XWPFDocument; 
import org.apache.poi.hwpf.model.StyleDescription; 
import org.apache.poi.xwpf.extractor.XWPFWordExtractor; 
import org.apache.poi.xwpf.usermodel.XWPFParagraph; 
import org.apache.poi.xwpf.usermodel.XWPFRun; 
import org.apache.poi.xwpf.usermodel.XWPFStyle; 
import org.apache.poi.xwpf.usermodel.XWPFStyles; 
import java.util.List; 

public class ReadFile 
{ 
public static void main(String[] args)throws Exception 
{ 

    XWPFDocument docx = new XWPFDocument(new FileInputStream("d:\\Profiles\\mehjain\\Desktop\\Test1.docx")); 

    List<XWPFParagraph> paragraphList = docx.getParagraphs(); 

    XWPFDocument document= new XWPFDocument(); 
    FileOutputStream out = new FileOutputStream(new File("d:\\Profiles\\mehjain\\Desktop\\Test2.docx")); 
    XWPFParagraph n = document.createParagraph(); 



    for (XWPFParagraph paragraph : paragraphList) 
    { 

     for(XWPFRun run1 : paragraph.getRuns()) 
     { 
     XWPFRun run=n.createRun(); 
     run.setText(run1.getText(0)); 
     run.setFontFamily(run1.getFontFamily()); 
     run.setBold(run1.isBold()); 
     run.setItalic(run1.isItalic()); 
     run.setStrike(run1.isStrike()); 
     run.setColor(run1.getColor()); 
     } 
     XWPFRun run=n.createRun(); 
     run.addCarriageReturn(); 
    } 
    document.write(out); 
    document.close(); 
    out.close(); 
    System.out.println("Test2.docx written successfully"); 
    } 
    } 
+0

将整个段落从Test1.docx复制到Test2.docx是一个选项吗?这可以使用'XWPFDocument'的方法'setParagraph(XWPFParagraph paragraph,int pos)'。 –

+0

你是什么意思,“我无法复制数字”?你的意思是编号清单(段落)?再说一遍:将Test1.docx中的整个段落复制到Test2.docx是否是一个选项?或者将Test1.docx中的多个段落的内容放入Test2.docx中的一个段落中? –

+0

假设 “1.简介”存在于Test1.Docx中。 所以,我只能复制Test2.docx中的“Introduction” –

回答

1

这可能帮助:

import java.io.File; 
import java.io.FileOutputStream; 

import org.apache.poi.xwpf.usermodel.VerticalAlign; 
import org.apache.poi.xwpf.usermodel.XWPFDocument; 
import org.apache.poi.xwpf.usermodel.XWPFParagraph; 
import org.apache.poi.xwpf.usermodel.XWPFRun; 

public class FontStyle 
{ 
    public static void main(String[] args)throws Exception 
    { 
    //Blank Document 
    XWPFDocument document= new XWPFDocument(); 

    //Write the Document in file system 
    FileOutputStream out = new FileOutputStream(
    new File("fontstyle.docx")); 

    //create paragraph 
    XWPFParagraph paragraph = document.createParagraph(); 

    //Set Bold an Italic 
    XWPFRun paragraphOneRunOne = paragraph.createRun(); 
    paragraphOneRunOne.setBold(true); 
    paragraphOneRunOne.setItalic(true); 
    paragraphOneRunOne.setText("Font Style"); 
    paragraphOneRunOne.addBreak(); 

    //Set text Position 
    XWPFRun paragraphOneRunTwo = paragraph.createRun(); 
    paragraphOneRunTwo.setText("Font Style two"); 
    paragraphOneRunTwo.setTextPosition(100); 

    //Set Strike through and Font Size and Subscript 
    XWPFRun paragraphOneRunThree = paragraph.createRun(); 
    paragraphOneRunThree.setStrike(true); 
    paragraphOneRunThree.setFontSize(20); 
    paragraphOneRunThree.setSubscript(
    VerticalAlign.SUBSCRIPT); 
    paragraphOneRunThree.setText(" Different Font Styles"); 

    document.write(out); 
    out.close(); 
    System.out.println("fontstyle.docx written successully"); 
    } 
} 

道具:http://www.tutorialspoint.com/apache_poi_word/apache_poi_word_font_style.htm

+0

实际上,我想读取一个word文档,并以与第一个文档中相同的样式将其写入另一个word文档。 假设第一份文件中的数据是: '我的名字是XYZ'。和'XYZ'以粗体显示。 所以我想'我的名字是XYZ'写在第二个文件中,'XYZ'以粗体显示。 –

0

您可以使用文件从Java复制方法。使用下面的代码片段:

Files.copy(new File("source path location"), new File("destination path location")); 

希望它能达到目的。

1

将一个Word docx中的整个段落复制到另一个段落会比将一个Word docx中的所有内容更改为另一个段落中的单个段落更简单。而且由于您声明源docx中有编号,所以需要整个段落,因为只有段落可以编号。

但要复制编号,还必须复制/word/numbering.xml。 因此,如果Test1.docx看起来是这样的: enter image description here

后处理下面的程序:

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import org.apache.poi.xwpf.usermodel.*; 

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTNumbering; 

import java.util.List; 
import java.lang.reflect.Field; 

public class CopyWordParagraphsDocToDoc { 
public static void main(String[] args)throws Exception { 

    XWPFDocument docx1 = new XWPFDocument(new FileInputStream("Test1.docx")); 
    XWPFNumbering numberingDocx1 = docx1.getNumbering(); 
    // get paragraphListDocx1 as a List of all paragraphs from docx1 
    List<XWPFParagraph> paragraphListDocx1 = docx1.getParagraphs(); 

    // get the numbering.xml from docx1 to docx2 
    // this is needed if some of the paragraphs from docx1 are numbered 
    XWPFDocument docx2= new XWPFDocument(); 
    if (numberingDocx1 != null) { 
    XWPFNumbering numberingDocx2 = docx2.createNumbering(); 
    try { 
    Field f = numberingDocx1.getClass().getDeclaredField("ctNumbering"); 
    f.setAccessible(true); 
    numberingDocx2.setNumbering((CTNumbering)f.get(numberingDocx1)); 
    } catch (NoSuchFieldException nsfex) { 
    } catch (IllegalAccessException iaex) { 
    } 
    } 

    // create a paragraph in docx2 
    XWPFParagraph paragraphDocx2 = docx2.createParagraph(); 
    XWPFRun run = paragraphDocx2.createRun(); 
    run.setText("This is from Test1.docx:"); 

    // this will copy all paragraphs from paragraphListDocx1 to docx2 
    for (XWPFParagraph paragraphDocx1 : paragraphListDocx1) { 
    paragraphDocx2 = docx2.createParagraph(); 
    docx2.setParagraph(paragraphDocx1, docx2.getPosOfParagraph(paragraphDocx2));    
    } 

    paragraphDocx2 = docx2.createParagraph(); 
    run = paragraphDocx2.createRun(); 
    run.setText("^-- this was from Test1.docx."); 


    FileOutputStream out = new FileOutputStream(new File("Test2.docx")); 
    docx2.write(out); 
    docx2.close(); 

    System.out.println("Test2.docx written successfully"); 
} 
} 

的Test2.docx看起来就像这样:

注意: 表格将不会被复制,因为我们只复制段落。 由于/word/media/*.*未被复制,图片将被破坏。 像“标题1”的特殊样式不会被复制,因为我们不复制/word/styles.xml