You are on page 1of 5

COMDOC ALGORITHMS

1. Read simple data from Docx


Lets have a word file as below

Now lets read it

1 package com.kscodes.test;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5
6 import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
7 import org.apache.poi.xwpf.usermodel.XWPFDocument;
8
9 public class ReadDocUsingPOI {
10 public static void main(String args[]) {
11
12 XWPFDocument document = null;
FileInputStream fileInputStream = null;
13
try {
14
15
16
File fileToBeRead = new
17
File("C:\\kscodes_temp\\SimpleFileToRead.docx");
18
fileInputStream = new FileInputStream(fileToBeRead);
19
document = new XWPFDocument(fileInputStream);
20
XWPFWordExtractor extractor = new XWPFWordExtractor(document);
21
22
System.out.println("The Contents of the Word File are ::");
23
System.out.println("--------------------------------------");
24
25
System.out.println(extractor.getText());
26
27
} catch (Exception e) {
28
System.out.println("We had an error while reading the Word Doc");
29
} finally {
30
try {
31
if (document != null) {
32
document.close();
33
}
34
if (fileInputStream != null) {
35
fileInputStream.close();
36
}
37
} catch (Exception ex) {
38
}
39
}
40
41
}
42
}
Output

2. Read table from Docx file


Now lets try to read a file which has table data in it. We will add some table contents to the above file
and again try to run the code to see the output
Output

As you can see that the XWPFWordExtractor.getText() will always return simple String that it reads.

You might also like