I'm using apache poi for iteration table in docx file. All works fine but if table in text box, my code don't see table - table.size() = 0
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){
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){
}
}
}
}
}
}
}
}
}
Following code is low level parsing a
*.docx
document and getting all tables in document body of it.The approach is using a org.apache.xmlbeans.XmlCursor and searching for all
w:tbl
elements indocument.xml
. If found add them to aList<CTTbl>
.Because a text box rectangle shape provides fall-back content in the
document.xml
, we need to skip themc:Fallback
elements. Else we would have the tables within the text boxes twice.At last we go through the
List<CTTbl>
and get the contents of all the tables.This code needs the full jar of all of the schemas
ooxml-schemas-1.3.jar
as mentioned in faq-N10025.