You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by vasavi <va...@gmail.com> on 2011/04/28 07:18:12 UTC

read table from word document using XWPF in Aapche poi

  hi,

i want to read a table data,  in word document  using XWPF APACHE  POI.

i have docx file .

i got the xml format of that table data but how to read that table in cell
wise.

can any body tell how to read the table using xwpf
 
i am using java


--
View this message in context: http://apache-poi.1045710.n5.nabble.com/read-table-from-word-document-using-XWPF-in-Aapche-poi-tp4345743p4345743.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: read table from word document using XWPF in Aapche poi

Posted by Jefin <pj...@gmail.com>.
Hi Vasavi,

you can try this code which i had to write for my work, hope it should help
you, incase of any doubts u can write me back 

"
public static void readWordDocument() { 
    try { 
            String fileName = "D:\\EC-4.docx"; 
          
            if(!(fileName.endsWith(".doc") || fileName.endsWith(".docx"))) { 
                    throw new FileFormatException(); 
            } else { 
            		
            		XWPFDocument doc = new XWPFDocument(new
FileInputStream(fileName));
                    
                    List<XWPFTable> table = doc.getTables();        
                    
                    for (XWPFTable xwpfTable : table) {
							List<XWPFTableRow> row = xwpfTable.getRows();
							for (XWPFTableRow xwpfTableRow : row) {
								List<XWPFTableCell> cell = xwpfTableRow.getTableCells();
								for (XWPFTableCell xwpfTableCell : cell) {
									if(xwpfTableCell!=null)
									{
										System.out.println(xwpfTableCell.getText());
										List<XWPFTable> itable = xwpfTableCell.getTables();
										if(itable.size()!=0)
										{
											for (XWPFTable xwpfiTable : itable) {
												List<XWPFTableRow> irow = xwpfiTable.getRows();
												for (XWPFTableRow xwpfiTableRow : irow) {
													List<XWPFTableCell> icell = xwpfiTableRow.getTableCells();
													for (XWPFTableCell xwpfiTableCell : icell) {
														if(xwpfiTableCell!=null)
														{  
															System.out.println(xwpfiTableCell.getText());
														}
													}
												}
											}
										}
									}
								}
							}
                    	}
            	}
    } catch(FileFormatException e) { 
            e.printStackTrace(); 
    } catch (FileNotFoundException e) { 
            e.printStackTrace(); 
    } catch (IOException e) { 
            e.printStackTrace(); 
    } 
   
  }						
"



--
View this message in context: http://apache-poi.1045710.n5.nabble.com/read-table-from-word-document-using-XWPF-in-Aapche-poi-tp4345743p5714894.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: read table from word document using XWPF in Aapche poi

Posted by vasavi <va...@gmail.com>.
thank u for ur replay

can u tell me how to read cells in a row.

i wrote the code like this , i got the text in only table.

 Iterator<XWPFTable> range=sampleDoc.getTablesIterator();

		 	  if(range.hasNext())
		 	  {
		 		 XWPFTable table=range.next(); 
		 		 System.out.println("   "+table.getText());
		 		
		 	  }
tell me how to read cells in a table 


--
View this message in context: http://apache-poi.1045710.n5.nabble.com/read-table-from-word-document-using-XWPF-in-Aapche-poi-tp4345743p4345990.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: read table from word document using XWPF in Aapche poi

Posted by Mark Beardsley <ma...@tiscali.co.uk>.
If you take a look at the javadoc for the XWPFDocument class, you should see
a method called getTablesIterator() which - if you call it - will return a
java.util.Iterator that allows you to step thruigh the tables in the Word
document one at a time.

Assuming you are familiar with the process of using Iterators, you will get
an instance of the XWPFTable class each time you call the next() method on
the Iterator. With the XWPFTable instance in hand, call the getRows()
method. This will return an instance of a class that implements the
java.util.List class and from which you should be able to recover another
Iterator object that will allow you access to the tables rows. Again, use
the common idiom to get an instance of the XWPFTableRow upon which you can
call the getTableCells() method. That call will return yet another List
which you can step through - again most likely using an Iterator - to get
access to the row's cells as instances of the XWPFTableCell class.

Your

Mark B

--
View this message in context: http://apache-poi.1045710.n5.nabble.com/read-table-from-word-document-using-XWPF-in-Aapche-poi-tp4345743p4345899.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org