0% found this document useful (0 votes)
59 views

Comdoc Algorithms: 1. Read Simple Data From

This document discusses how to summarize tables from Docx files using POI APIs in Java. It first shows how to read simple text content from a Docx file, and then enhances the example file to include a table. When the code is run again, it demonstrates that XWPFWordExtractor will return a simple string of the file contents, without preserving the table formatting. The document thus serves as an example of using POI to extract text from Docx files, including files with tables, in Java code.

Uploaded by

sudhakar kethana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Comdoc Algorithms: 1. Read Simple Data From

This document discusses how to summarize tables from Docx files using POI APIs in Java. It first shows how to read simple text content from a Docx file, and then enhances the example file to include a table. When the code is run again, it demonstrates that XWPFWordExtractor will return a simple string of the file contents, without preserving the table formatting. The document thus serves as an example of using POI to extract text from Docx files, including files with tables, in Java code.

Uploaded by

sudhakar kethana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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