Skip to content

Commit 4f1ac2e

Browse files
nguyennamthaizhendrikse
authored andcommitted
Apache POI Support for MS Word (eugenp#908)
* Initial commit for Apache POI support for MS Word * Relocate the POI Word project * Add Apache POI to the repo
1 parent 0467e93 commit 4f1ac2e

8 files changed

Lines changed: 190 additions & 0 deletions

File tree

‎apache-poi/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.docx

‎apache-poi/pom.xml‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>apache-poi</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
8+
<properties>
9+
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
10+
<junit.version>4.12</junit.version>
11+
<poi.version>3.15</poi.version>
12+
</properties>
13+
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>org.apache.maven.plugins</groupId>
18+
<artifactId>maven-compiler-plugin</artifactId>
19+
<version>${maven-compiler-plugin.version}</version>
20+
<configuration>
21+
<source>1.8</source>
22+
<target>1.8</target>
23+
</configuration>
24+
</plugin>
25+
</plugins>
26+
</build>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>junit</groupId>
31+
<artifactId>junit</artifactId>
32+
<version>${junit.version}</version>
33+
<scope>test</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.apache.poi</groupId>
37+
<artifactId>poi-ooxml</artifactId>
38+
<version>${poi.version}</version>
39+
</dependency>
40+
</dependencies>
41+
</project>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.baeldung.poi.word;
2+
3+
import java.io.FileOutputStream;
4+
import java.io.IOException;
5+
import java.net.URISyntaxException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
import java.util.stream.Collectors;
10+
import java.util.stream.Stream;
11+
12+
import org.apache.poi.util.Units;
13+
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
14+
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
15+
import org.apache.poi.xwpf.usermodel.XWPFDocument;
16+
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
17+
import org.apache.poi.xwpf.usermodel.XWPFRun;
18+
19+
public class WordDocument {
20+
public static String logo = "logo-leaf.png";
21+
public static String paragraph1 = "poi-word-para1.txt";
22+
public static String paragraph2 = "poi-word-para2.txt";
23+
public static String paragraph3 = "poi-word-para3.txt";
24+
public static String output = "rest-with-spring.docx";
25+
26+
public void handleSimpleDoc() throws Exception {
27+
XWPFDocument document = new XWPFDocument();
28+
29+
XWPFParagraph title = document.createParagraph();
30+
title.setAlignment(ParagraphAlignment.CENTER);
31+
XWPFRun titleRun = title.createRun();
32+
titleRun.setText("Build Your REST API with Spring");
33+
titleRun.setColor("009933");
34+
titleRun.setBold(true);
35+
titleRun.setFontFamily("Courier");
36+
titleRun.setFontSize(20);
37+
38+
XWPFParagraph subTitle = document.createParagraph();
39+
subTitle.setAlignment(ParagraphAlignment.CENTER);
40+
XWPFRun subTitleRun = subTitle.createRun();
41+
subTitleRun.setText("from HTTP fundamentals to API Mastery");
42+
subTitleRun.setColor("00CC44");
43+
subTitleRun.setFontFamily("Courier");
44+
subTitleRun.setFontSize(16);
45+
subTitleRun.setTextPosition(20);
46+
subTitleRun.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
47+
48+
XWPFParagraph image = document.createParagraph();
49+
image.setAlignment(ParagraphAlignment.CENTER);
50+
XWPFRun imageRun = image.createRun();
51+
imageRun.setTextPosition(20);
52+
Path imagePath = Paths.get(ClassLoader.getSystemResource(logo).toURI());
53+
imageRun.addPicture(Files.newInputStream(imagePath), XWPFDocument.PICTURE_TYPE_PNG, imagePath.getFileName().toString(), Units.toEMU(50), Units.toEMU(50));
54+
55+
XWPFParagraph sectionTitle = document.createParagraph();
56+
XWPFRun sectionTRun = sectionTitle.createRun();
57+
sectionTRun.setText("What makes a good API?");
58+
sectionTRun.setColor("00CC44");
59+
sectionTRun.setBold(true);
60+
sectionTRun.setFontFamily("Courier");
61+
62+
XWPFParagraph para1 = document.createParagraph();
63+
para1.setAlignment(ParagraphAlignment.BOTH);
64+
String string1 = convertTextFileToString(paragraph1);
65+
XWPFRun para1Run = para1.createRun();
66+
para1Run.setText(string1);
67+
68+
XWPFParagraph para2 = document.createParagraph();
69+
para2.setAlignment(ParagraphAlignment.RIGHT);
70+
String string2 = convertTextFileToString(paragraph2);
71+
XWPFRun para2Run = para2.createRun();
72+
para2Run.setText(string2);
73+
para2Run.setItalic(true);
74+
75+
XWPFParagraph para3 = document.createParagraph();
76+
para3.setAlignment(ParagraphAlignment.LEFT);
77+
String string3 = convertTextFileToString(paragraph3);
78+
XWPFRun para3Run = para3.createRun();
79+
para3Run.setText(string3);
80+
81+
FileOutputStream out = new FileOutputStream(output);
82+
document.write(out);
83+
out.close();
84+
document.close();
85+
}
86+
87+
public String convertTextFileToString(String fileName) {
88+
try (Stream<String> stream = Files.lines(Paths.get(ClassLoader.getSystemResource(fileName).toURI()))) {
89+
return stream.collect(Collectors.joining(" "));
90+
} catch (IOException | URISyntaxException e) {
91+
e.printStackTrace();
92+
}
93+
return null;
94+
}
95+
}
782 Bytes
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I published the first REST with Spring tutorials over four years ago, and after 300,000 readers and over a thousand personal questions over email, I am finally realizing that there is a huge education gap in the ecosystem.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
I had to learn what makes a good API is the hard way, and I couldn’t find anything out there to help me speed things up.
2+
It took me 3 years, more than 10 production grade APIs built from scratch and a decent number of mistakes to get to a mature understanding of how an API should really be done well.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Sure, there were tutorials and random code samples out there, but nothing coherent.
2+
Nothing start to finish to act as a guide through the challenges of building a mature API with Spring.
3+
The educational gap between beginner and the API professional remains vast.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.poi.word;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
import java.util.List;
10+
11+
import org.apache.poi.xwpf.usermodel.XWPFDocument;
12+
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
13+
import org.apache.poi.xwpf.usermodel.XWPFRun;
14+
import org.junit.BeforeClass;
15+
import org.junit.Test;
16+
17+
public class WordTest {
18+
static WordDocument wordDocument;
19+
20+
@BeforeClass
21+
public static void generateMSWordFile() throws Exception {
22+
WordTest.wordDocument = new WordDocument();
23+
wordDocument.handleSimpleDoc();
24+
}
25+
26+
@Test
27+
public void whenParsingOutputDocument_thenCorrect() throws Exception {
28+
Path msWordPath = Paths.get(WordDocument.output);
29+
XWPFDocument document = new XWPFDocument(Files.newInputStream(msWordPath));
30+
List<XWPFParagraph> paragraphs = document.getParagraphs();
31+
document.close();
32+
33+
XWPFParagraph title = paragraphs.get(0);
34+
XWPFRun titleRun = title.getRuns().get(0);
35+
assertEquals("Build Your REST API with Spring", title.getText());
36+
assertEquals("009933", titleRun.getColor());
37+
assertTrue(titleRun.isBold());
38+
assertEquals("Courier", titleRun.getFontFamily());
39+
assertEquals(20, titleRun.getFontSize());
40+
41+
assertEquals("from HTTP fundamentals to API Mastery", paragraphs.get(1).getText());
42+
assertEquals("What makes a good API?", paragraphs.get(3).getText());
43+
assertEquals(wordDocument.convertTextFileToString(WordDocument.paragraph1), paragraphs.get(4).getText());
44+
assertEquals(wordDocument.convertTextFileToString(WordDocument.paragraph2), paragraphs.get(5).getText());
45+
assertEquals(wordDocument.convertTextFileToString(WordDocument.paragraph3), paragraphs.get(6).getText());
46+
}
47+
}

0 commit comments

Comments
 (0)