Apache POI is a Java library for working with the various file formats based on the Office Open XML standards (OOXML) and Microsoft’s OLE 2 Compound Document format (OLE2).
This tutorial focuses on the support of Apache POI for Microsoft Word, the most commonly used Office file format. It walks through steps needed to format and generate an MS Word file and how to parse this file.
We’ll collect the contents of three text files and write them into an MS Word file – named rest-with-spring.docx.
In addition, the logo-leaf.png file is used to insert an image into that new file. All these files do exist on the classpath and are represented by several static variables:
For those who are curious, contents of these resource files in the repository, whose link is given in the last section of this tutorial, are extracted from this course page here on the site.
This method extracts contents contained in a text file located on the classpath, whose name is the passed-in String argument. Then, it concatenates lines in this file and returns the joining String.
This section gives instructions on how to format and generate a Microsoft Word file. Prior to working on any part of the file, we need to have an XWPFDocument instance:
In order to create the title, we need to first instantiate the XWPFParagraph class and set the alignment on the new object:
1 2
XWPFParagraph title = document.createParagraph(); title.setAlignment(ParagraphAlignment.CENTER);Copy
The content of a paragraph needs to be wrapped in an XWPFRun object. We may configure this object to set a text value and its associated styles:
1 2 3 4 5 6
XWPFRun titleRun = title.createRun(); titleRun.setText("Build Your REST API with Spring"); titleRun.setColor("009933"); titleRun.setBold(true); titleRun.setFontFamily("Courier"); titleRun.setFontSize(20);Copy
One should be able to infer the purposes of the set-methods from their names.
In a similar way we create an XWPFParagraph instance enclosing the subtitle:
An image also needs to be wrapped in an XWPFParagraph instance. We want the image to be horizontally centered and placed under the subtitle, thus the following snippet must be put below the code given above:
It is apparent that the creation of a paragraph is similar to the creation of the title or subtitle. The only difference here is the use of the helper method instead of hard-coded strings.
In a similar way, we can create two other paragraphs using contents from files poi-word-para2.txt and poi-word-para3.txt:
This field is used to reference to an instance of the class that encloses all the code fragments shown in sections 3 and 4.
Before parsing and testing, we need to initialize the static variable declared right above and generate the rest-with-spring.docx file in the current working directory by invoking the handleSimpleDoc method:
1 2 3 4 5
@BeforeClass publicstaticvoidgenerateMSWordFile()throws Exception { WordTest.wordDocument = new WordDocument(); wordDocument.handleSimpleDoc(); }Copy
Let’s move on to the final step: parsing the MS Word file and the verification of the outcome.
Next, let’s make sure that the content and style of the title is the same as what we have set before:
1 2 3 4 5 6 7 8
XWPFParagraph title = paragraphs.get(0); XWPFRun titleRun = title.getRuns().get(0);
assertEquals("Build Your REST API with Spring", title.getText()); assertEquals("009933", titleRun.getColor()); assertTrue(titleRun.isBold()); assertEquals("Courier", titleRun.getFontFamily()); assertEquals(20, titleRun.getFontSize());Copy
For the sake of simplicity, we just validate the contents of other parts of the file, leaving out the styles. The verification of their styles is similar to what we have done with the title:
1 2 3 4 5 6 7 8 9
assertEquals("from HTTP fundamentals to API Mastery", paragraphs.get(1).getText()); assertEquals("What makes a good API?", paragraphs.get(3).getText()); assertEquals(wordDocument.convertTextFileToString (WordDocument.paragraph1), paragraphs.get(4).getText()); assertEquals(wordDocument.convertTextFileToString (WordDocument.paragraph2), paragraphs.get(5).getText()); assertEquals(wordDocument.convertTextFileToString (WordDocument.paragraph3), paragraphs.get(6).getText());Copy
Now we can be confident that the creation of the rest-with-spring.docx file has been successful.
This tutorial introduced Apache POI support for the Microsoft Word format. It went through steps needed to generate an MS Word file and to verify its contents.