- Apache POI Word - Home
- Apache POI Word - Overview
- Apache POI Word - Installation
- Apache POI Word - Core Classes
- Apache POI Word - Document
- Apache POI Word - Paragraph
- Apache POI Word - Borders
- Apache POI Word - Tables
- Apache POI Word - Font & Alignment
- Apache POI Word - Text Extraction
- Apache POI Word Resources
- Apache POI Word - Quick Guide
- Apache POI Word - Useful Resources
- Apache POI Word - Discussion
Apache POI Word - Document
Here the term 'document' refers to a MS-Word file. After completion of this chapter, you will be able to create new documents and open existing documents using your Java program.
Create a Document
First of all, let us create a Document using the referenced classes discussed in the earlier chapters. By following the previous chapter, create a Document first, and then we can save to disk.
The following code snippet is used to create a document −
//Blank Document
XWPFDocument document = new XWPFDocument();
//Write the Document in file system
FileOutputStream out = new FileOutputStream( new File("example.docx"));
document.write(out);
Example - Creating a Blank Document
The following simple program is used to create a blank MS-Word document −
ApachePoiDocDemo.java
package com.tutorialspoint;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class ApachePoiDocDemo {
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("example.docx"));
document.write(out);
out.close();
document.close();
System.out.println("example.docx written successully");
}
}
Output
If your system environment is configured with the POI library, it will compile and execute to generate a blank Word document file named example.docx in your current directory and display the following output in the command prompt −
example.docx written successfully
Advertisements