6

I have a word document which may have n number of tables. The table is identified by the table name which is written in the 1st cell as heading. Now i have to find the table with table name and write in one of the cell of that table. I tried using apache-poi for the same but unable to figure out how to use it for my purpose. Please refer to the attached screen shot, if i am not able to explain how the document looks like.

Thanks as seen in screenshot name of tables are S1 and S2

    String fileName = "E:\\a1.doc";  

    if (args.length > 0) {  
        fileName = args[0];  
    }  

    InputStream fis = new FileInputStream(fileName);  
    POIFSFileSystem fs = new POIFSFileSystem(fis);  
    HWPFDocument doc = new HWPFDocument(fs);  

    Range range = doc.getRange(); 
    for (int i=0; i<range.numParagraphs(); i++){ 
       Paragraph tablePar = range.getParagraph(i);

        if (tablePar.isInTable()) {  
            Table table = range.getTable(tablePar);  
            for (int rowIdx=0; rowIdx<table.numRows(); rowIdx++) {  

                for (int colIdx=0; colIdx<row.numCells(); colIdx++) {  
                    TableCell cell = row.getCell(colIdx);  
                    System.out.println("column="+cell.getParagraph(0).text());  
                }  
            }  
        }  
    } 

this is what i have tried, but this reads only the 1st table.

3
  • @ADG, you should post that as a response. POI is definitively the most mature word library. Commented Sep 24, 2012 at 9:27
  • removed selenium tags.. don't know why you linked it Commented Sep 24, 2012 at 9:34
  • @tom pasted as answer ;) Commented Sep 24, 2012 at 10:41

2 Answers 2

3

I've found u get misunderstanding in poi. If u just meant to read a table.Just use the TableIterator to fetch the table's content or u will get an exception with not start of table.

I suppose there is only one paragraph in every table cell.

    InputStream fis = new FileInputStream(fileName);  
    POIFSFileSystem fs = new POIFSFileSystem(fis);  
    HWPFDocument doc = new HWPFDocument(fs);  

    Range range = doc.getRange();
    TableIterator itr = new TableIterator(range);
    while(itr.hasNext()){
        Table table = itr.next();
        for(int rowIndex = 0; rowIndex < table.numRows(); rowIndex++){
            TableRow row = table.getRow(rowIndex);
            for(int colIndex = 0; colIndex < row.numCells(); colIndex++){
                TableCell cell = row.getCell(colIndex);
                System.out.println(cell.getParagraph(0).text());
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

2

I think Apache POI is the way to go. It's not well documented, but the time spent on research how to use it may be worth it. Word document is basically a hierarchical (tree) structure which you need to traverse and find the data you need.

1 Comment

I tried using Apache POI but unable to find a specific table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.