Read Word document using Apache POI (Java)

| By Webner

Read Word document using Apache POI (Java) :

To read any document (.doc or .docx) or excel document in java, there are several libraries but Apache POI is pretty good. Using this library we can read word documents line by line.

Required Jar files:

  1. Poi.jar
  2. Poi-ooxml.jar
  3. Poi-ooxml-schemas.jar
  4. Poi-scratchpad.jar
  5. Xmlbeans.jar

You can download all these jars from below link:

Here is the simple program to read a doc file using Apache POI:

   public class FileReader{
 * Read any type of word or excel document
 * @param filename
public void  readDocument(String filename){
        //variable declaration
FileInputStream fis = new FileInputStream(filename);
XWPFDocument docx = new XWPFDocument(fis);
List<XWPFParagraph> para= docx.getParagraphs();
List<XWPFTable> tbl = docx.getTables();
Iterator<IBodyElement> iter = docx.getBodyElementsIterator();
List<String[]> varArray = new ArrayList<String[]>();
while (iter.hasNext()) {
IBodyElement elem =;
if (elem instanceof XWPFParagraph) {
String st = "image";
for (XWPFRun run : para.get(countpara).getRuns()) {
for (XWPFPicture pic : run.getEmbeddedPictures()) {
picdata = pic.getPictureData().getFileName();
detectFileExtension(varArray, st);
String dataStyle = para.get(countpara).getStyle();
if (elem instanceof XWPFTable) {
int numberOfRows = 
readTableData(tbl, varArray, st, numberOfRows);
} // End of if
} // End of while
} // End of function
private void readTableData(List<XWPFTable> tbl, List<String[]> varArray, String st, int numberOfRows) {
numberOfColumns =tbl.get(counttbl).getRow(i).getTableICells().size();
for(int j=0;j<numberOfColumns;j++)
cellData= tbl.get(counttbl).getRow(i).getTableCells().get(j).getText();
varArray.add(new String[] {st,Integer.toString(tableNo),tr,Integer.toString(i),td, cellData });
private void detectFileExtension(List<String[]> varArray, String st) {
varArray.add(new String[] {st,picdata});
else if(picdata.contains(".jpg")){
varArray.add(new String[] {st,picdata});
else if(picdata.contains(".jpeg")){
varArray.add(new String[] {st,picdata});

In above program there are mainly three classes used which are :

  • 1. XWPFParagraph :: To read text data or style of data in the document.
  • 2. XWPFTable :: To read table data from the document.
  • 3. XWPFPicture :: To read image name or data from the document.

Here are the sample screenshots of a word document that is being read by these classes of Apache POI library:

Leave a Reply

Your email address will not be published. Required fields are marked *