2016-09-29 105 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