Tuesday, December 25, 2012

How to create a Word Document

just download the apache poi.jar


package org.IGNOU.ePortfolio.Action;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xwpf.usermodel.Borders;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
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;

/**
 *
 * @author Amit
 */
public class CreateWordDoc {

     public static void main(String[] args) throws IOException {
        XWPFDocument document = new XWPFDocument();

        XWPFParagraph paragraphOne = document.createParagraph();
        paragraphOne.setAlignment(ParagraphAlignment.CENTER);
        paragraphOne.setBorderBottom(Borders.SINGLE);
        paragraphOne.setBorderTop(Borders.SINGLE);
        paragraphOne.setBorderRight(Borders.SINGLE);
        paragraphOne.setBorderLeft(Borders.SINGLE);
        paragraphOne.setBorderBetween(Borders.SINGLE);

        XWPFRun paragraphOneRunOne = paragraphOne.createRun();
        paragraphOneRunOne.setBold(true);
        paragraphOneRunOne.setItalic(true);
        paragraphOneRunOne.setText("Hello world! This is paragraph one!");
        paragraphOneRunOne.addBreak();

        XWPFRun paragraphOneRunTwo = paragraphOne.createRun();
        paragraphOneRunTwo.setText("Run two!");
        paragraphOneRunTwo.setTextPosition(100);

        XWPFRun paragraphOneRunThree = paragraphOne.createRun();
        paragraphOneRunThree.setStrike(true);
        paragraphOneRunThree.setFontSize(20);
        paragraphOneRunThree.setSubscript(VerticalAlign.SUBSCRIPT);
        paragraphOneRunThree.setText(" More text in paragraph one...");

        XWPFParagraph paragraphTwo = document.createParagraph();
        paragraphTwo.setAlignment(ParagraphAlignment.DISTRIBUTE);
        paragraphTwo.setIndentationRight(200);
        XWPFRun paragraphTwoRunOne = paragraphTwo.createRun();
        paragraphTwoRunOne.setText("And this is paragraph two.");

        FileOutputStream outStream = null;
        try {
            outStream = new FileOutputStream("C:/Users/Amit/Downloads/Amit.doc");
        } catch (FileNotFoundException e) {
        }

        try {
            document.write(outStream);
            outStream.close();
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
    }

}

2 comments:

  1. i have this error, when i execute the code above. So can you help me.
    Error:
    Exception in thread "main" java.lang.NoClassDefFoundError: org.apache.xmlbeans.XmlException
    at java.lang.J9VMInternals.verifyImpl(Native Method)
    at java.lang.J9VMInternals.verify(J9VMInternals.java:69)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:131)
    at ExcelReader.main(ExcelReader.java:71)
    Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:496)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:631)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:597)
    ... 4 more

    ReplyDelete
  2. sorry for the late reply. there are some library missing in application please verify the org.apache.xmlbeans.XmlException in your all library registered with if not found download from http://www.java2s.com/Code/Jar/x/Downloadxmlbeansxmlpublic240jar.htm

    hope your problem get solved

    ReplyDelete