Apache POI Word – Overview

Apache POI Word – Overview ”; Previous Next Many a time, a software application is required to generate reference documents in Microsoft Word file format. Sometimes, an application is even expected to receive Word files as input data. Any Java programmer who wants to produce MS-Office files as output must use a predefined and read-only API to do so. What is Apache POI? Apache POI is a popular API that allows programmers to create, modify, and display MS-Office files using Java programs. It is an open source library developed and distributed by Apache Software Foundation to design or modify MS-Office files using Java program. It contains classes and methods to decode the user input data or a file into MS-Office documents. Components of Apache POI Apache POI contains classes and methods to work on all OLE2 Compound documents of MS-Office. The list of components of this API is given below − POIFS (Poor Obfuscation Implementation File System) − This component is the basic factor of all other POI elements. It is used to read different files explicitly. HSSF (Horrible SpreadSheet Format) − It is used to read and write .xls format of MS-Excel files. XSSF (XML SpreadSheet Format) − It is used for .xlsx file format of MS-Excel. HPSF (Horrible Property Set Format) − It is used to extract property sets of the MS-Office files. HWPF (Horrible Word Processor Format) − It is used to read and write .doc extension files of MS-Word. XWPF (XML Word Processor Format) − It is used to read and write .docx extension files of MS-Word. HSLF (Horrible Slide Layout Format) − It is used to read, create, and edit PowerPoint presentations. HDGF (Horrible DiaGram Format) − It contains classes and methods for MS-Visio binary files. HPBF (Horrible PuBlisher Format) − It is used to read and write MS-Publisher files. This tutorial guides you through the process of working on MS-Word files using Java. Therefore the discussion is confined to HWPF and XWPF components. Note − OLDER VERSIONS OF POI SUPPORT BINARY FILE FORMATS SUCH AS DOC, XLS, PPT, ETC. VERSION 3.5 ONWARDS, POI SUPPORTS OOXML FILE FORMATS OF MS-OFFICE SUCH AS DOCX, XLSX, PPTX, ETC. Print Page Previous Next Advertisements ”;

Apache POI Word – Home

Apache POI Word Tutorial PDF Version Quick Guide Resources Job Search Discussion This tutorial provides a basic understanding of Apache POI library and its features. Here we will learn how to read, write, and manage MS-Word documents using Java programs. Audience This tutorial is designed for the readers working on Java and especially those who want to create, read, write, and modify Word files using Java. Prerequisites A general awareness of Java programming with JDK 1.5 or later versions and IO concepts in Java are the only prerequisites to understand this tutorial. Print Page Previous Next Advertisements ”;

Apache POI Word – Tables

Apache POI Word – Tables ”; Previous Next In this chapter, you will learn how to create a table of data in a document. You can create a table data by using XWPFTable class. By adding each Row to table and adding each cell to Row, you will get table data. Create Table The following code is used to creating table in a document − import java.io.File; import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableRow; public class CreateTable { 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(“create_table.docx”)); //create table XWPFTable table = document.createTable(); //create first row XWPFTableRow tableRowOne = table.getRow(0); tableRowOne.getCell(0).setText(“col one, row one”); tableRowOne.addNewTableCell().setText(“col two, row one”); tableRowOne.addNewTableCell().setText(“col three, row one”); //create second row XWPFTableRow tableRowTwo = table.createRow(); tableRowTwo.getCell(0).setText(“col one, row two”); tableRowTwo.getCell(1).setText(“col two, row two”); tableRowTwo.getCell(2).setText(“col three, row two”); //create third row XWPFTableRow tableRowThree = table.createRow(); tableRowThree.getCell(0).setText(“col one, row three”); tableRowThree.getCell(1).setText(“col two, row three”); tableRowThree.getCell(2).setText(“col three, row three”); document.write(out); out.close(); System.out.println(“create_table.docx written successully”); } } Save the above code in a file named CreateTable.java. Compile and execute it from the command prompt as follows − $javac CreateTable.java $java CreateTable It generates a Word file named createtable.docx in your current directory and display the following output on the command prompt − createtable.docx written successfully The createtable.docx file looks as follows − Print Page Previous Next Advertisements ”;

Apache POI Word – Document

Apache POI Word – Document ”; Previous Next 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 Blank Document The following simple program is used to create a blank MS-Word document − import java.io.File; import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.XWPFDocument; public class CreateDocument { 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(“createdocument.docx”)); document.write(out); out.close(); System.out.println(“createdocument.docx written successully”); } } Save the above Java code as CreateDocument.java, and then compile and execute it from the command prompt as follows − $javac CreateDocument.java $java CreateDocument If your system environment is configured with the POI library, it will compile and execute to generate a blank Word document file named createdocument.docx in your current directory and display the following output in the command prompt − createdocument.docx written successfully Print Page Previous Next Advertisements ”;

Apache POI Word – Core Classes

Apache POI Word – Core Classes ”; Previous Next This chapter takes you through the classes and methods of Apache POI for managing a Word document. Document This is a marker interface (interface do not contain any methods), that notifies that the implemented class can be able to create a word document. XWPFDocument This is a class under org.apache.poi.xwpf.usermodel package. It is used to create MS-Word Document with .docx file format. Class Methods Sr.No. Method & Description 1 commit() Commits and saves the document. 2 createParagraph() Appends a new paragraph to this document. 3 createTable() Creates an empty table with one row and one column as default. 4 createTOC() Creates a table of content for Word document. 5 getParagraphs() Returns the paragraph(s) that holds the text of the header or footer. 6 getStyle() Returns the styles object used. For the remaining methods of this class, refer the complete API document at − Package org.apache.poi.openxml4j.opc.internal. XWPFParagraph This is a class under org.apache.poi.xwpf.usermodel package and is used to create paragraph in a word document. This instance is also used to add all types of elements into word document. Class Methods Sr.No. Method & Description 1 createRun() Appends a new run to this paragraph. 2 getAlignment() Returns the paragraph alignment which shall be applied to the text in this paragraph. 3 setAlignment(ParagraphAlignment align) Specifies the paragraph alignment which shall be applied to the text in this paragraph. 4 setBorderBottom(Borders border) Specifies the border which shall be displayed below a set of paragraphs, which have the same set of paragraph border settings. 5 setBorderLeft(Borders border) Specifies the border which shall be displayed on the left side of the page around the specified paragraph. 6 setBorderRight(Borders border) Specifies the border which shall be displayed on the right side of the page around the specified paragraph. 7 setBorderTop(Borders border) Specifies the border which shall be displayed above a set of paragraphs which have the same set of paragraph border settings. For the remaining methods of this class, refer the complete API document at − POI API Documentation XWPFRun This is a class under org.apache.poi.xwpf.usermodel package and is used to add a region of text to the paragraph. Class Methods Sr.No. Method & Description 1 addBreak() Specifies that a break shall be placed at the current location in the run content. 2 addTab() Specifies that a tab shall be placed at the current location in the run content. 3 setColor(java.lang.String rgbStr) Sets text color. 4 setFontSize(int size) Specifies the font size which shall be applied to all noncomplex script characters in the content of this run when displayed. 5 setText(java.lang.String value) Sets the text of this text run. 6 setBold(boolean value) Specifies whether the bold property shall be applied to all non-complex script characters in the content of this run when displayed in a document. For the remaining methods of this class, refer the complete API document at − POI API Documentation XWPFStyle This is a class under org.apache.poi.xwpf.usermodel package and is used to add different styles to the object elements in a word document. Class Methods Sr.No. Method & Description 1 getNextStyleID() It is used to get StyleID of the next style. 2 getStyleId() It is used to get StyleID of the style. 3 getStyles() It is used to get styles. 4 setStyleId(java.lang.String styleId) It is used to set styleID. For the remaining methods of this class, refer the complete API document at − POI API Documentation XWPFTable This is a class under org.apache.poi.xwpf.usermodel package and is used to add table data into a word document. Class Methods Sr.No. Method & Description 1 addNewCol() Adds a new column for each row in this table. 2 addRow(XWPFTableRow row, int pos) Adds a new Row to the table at position pos. 3 createRow() Creates a new XWPFTableRow object with as many cells as the number of columns defined in that moment. 4 setWidth(int width) Sets the width of the column. For the remaining methods of this class, refer the complete API document at: POI API Documentation XWPFWordExtractor This is a class under org.apache.poi.xwpf.extractor package. It is a basic parser class used to extract the simple text from a Word document. Class Methods Sr.No. Method & Description 1 getText() Retrieves all the text from the document. For the remaining methods of this class, refer the complete API document at: POI API Documentation Print Page Previous Next Advertisements ”;