In this tutorial, we will discuss how to write in a Microsoft Word document.
If you are not familiar with library linking, follow these steps.
Create a directory, lib, in your project. Put the jars in the lib directory. Right-click on the project, select Properties, go to Java Build Path, click on Add Jars and browse the lib directory you have created. It's done!
Create an instance of XWPFDocument class for .docx file.
Dependencies
First, download the dependency files as mentioned in the Introduction to Apache POI For Manipulation of MS Office Documents with Java tutorial. After downloading the libraries, you have to add them to JAVA_PATH. Alternatively, you can create an Eclipse project and add them to the project as a library. You are being suggested to add the libraries as an internal library instead of an external library.If you are not familiar with library linking, follow these steps.
Create a directory, lib, in your project. Put the jars in the lib directory. Right-click on the project, select Properties, go to Java Build Path, click on Add Jars and browse the lib directory you have created. It's done!
Explanation of Source Code
Create an instance of FileOutputStream for the Demo.xlsx file.Create an instance of XWPFDocument class for .docx file.
Try the following code.
package com.bunks.demo.poi;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class WordDocumentCreationDemo {
public static void main(String[] args) {
XWPFDocument xwpfDocument = new XWPFDocument();
try {
OutputStream outputStream = new FileOutputStream("Demo.docx");
xwpfDocument.write(outputStream);
xwpfDocument.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.