Tuesday 10 September 2013

How to add text to a word file using Java

Purpose: How to add text to a word file using Java


Following code will help you to Add text to a Word file programatically.

import java.io.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
public class AddToWord
{
    public static void main(String[] args) throws IOException, InvalidFormatException
    {
        XWPFDocument document = new XWPFDocument();
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText("Sample Text to add a word file programatically");
        run.setFontSize(33);
        FileOutputStream out = new FileOutputStream("Success.doc");
        document.write(out);
        out.close();
    }
}

No comments:

Post a Comment