In previous post I explained about the reading of text from table having only one paragraph. But always this is not the case. Lot of documents daily we are processing has multiple paragraphs in cell. By using the method described in earlier we will get all the text from the cell. But we will miss the formatting of the paragraphs. If we want to get the individual paragraphs from the cell we can use the below line of code.
for ( XWPFParagraph para : cell.getParagraphs() )
{
}
The above for loop will give the one para at every iteration from the cell in the order they are typed in table cell.
The consolidated code to get all the text from the table with paragraphs is
fis = new FileInputStream("D:/POI/ex1.docx");
doc = new XWPFDocument(fis);
tables = doc.getTables();
for ( XWPFTable table : tables )
{
for ( XWPFTableRow row : table.getRows() )
{
for ( XWPFTableCell cell : row.getTableCells() )
{
for ( XWPFParagraph para : cell.getParagraphs() )
{
System.out.println(para.getText());
}
}
System.out.println("");
}
}
for ( XWPFParagraph para : cell.getParagraphs() )
{
}
The above for loop will give the one para at every iteration from the cell in the order they are typed in table cell.
The consolidated code to get all the text from the table with paragraphs is
fis = new FileInputStream("D:/POI/ex1.docx");
doc = new XWPFDocument(fis);
tables = doc.getTables();
for ( XWPFTable table : tables )
{
for ( XWPFTableRow row : table.getRows() )
{
for ( XWPFTableCell cell : row.getTableCells() )
{
for ( XWPFParagraph para : cell.getParagraphs() )
{
System.out.println(para.getText());
}
}
System.out.println("");
}
}
merci frér
ReplyDeleteHey I am new to java. I am trying to format a word.docx file to extract the data of two particular tables, whose heading is always going to be constant across all the documents that i need to format. How can I do that? A little help would be appreciated.
ReplyDelete