Example usage for org.apache.poi.xwpf.usermodel XWPFTableCell getTables

List of usage examples for org.apache.poi.xwpf.usermodel XWPFTableCell getTables

Introduction

In this page you can find the example usage for org.apache.poi.xwpf.usermodel XWPFTableCell getTables.

Prototype

public List<XWPFTable> getTables() 

Source Link

Usage

From source file:com.deepoove.poi.resolver.TemplateResolver.java

License:Apache License

public static List<ElementTemplate> parseTable(XWPFTable table) {
    if (null == table)
        return null;
    List<ElementTemplate> rts = new ArrayList<ElementTemplate>();
    List<XWPFTableRow> rows = table.getRows();
    if (null != rows) {
        for (XWPFTableRow row : rows) {
            List<XWPFTableCell> cells = row.getTableCells();
            if (null != cells) {
                for (XWPFTableCell cell : cells) {
                    // cell?
                    // CellTemplate parseCell = parseCell(cell);
                    // if (null != parseCell) {
                    // rts.add(parseCell);
                    // } else {
                    rts.addAll(parseParagraph(cell.getParagraphs()));
                    rts.addAll(parseTable(cell.getTables()));
                }//from w w w. j a v  a2s  .  c o m
            }
        }
    }
    return rts;
}

From source file:myexamples.WordDocsExamples.Test1.java

public static void simplepartsReading() throws IOException {
    JFileChooser chooser = new JFileChooser();
    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        System.out.println(chooser.getSelectedFile().getName());
        FileInputStream fis = new FileInputStream(chooser.getSelectedFile());
        XWPFDocument doc = new XWPFDocument(fis);
        XWPFWordExtractor extract = new XWPFWordExtractor(doc);
        //System.out.println(extract.getText());
        List<XWPFParagraph> pList = doc.getParagraphs();
        List<XWPFTable> tList = doc.getTables();

        System.out.println("Number of Paragraphs=" + pList.size());
        System.out.println("Number of Tables=" + tList.size());
        List<XWPFTableRow> rList;
        List<XWPFTableCell> cList;
        List<XWPFParagraph> rcpList;
        int tCount = 0, rCount = 0, cCount = 0, rcpCount = 0, dummCounter = 0;
        WordReference wordReference = new WordReference();
        for (XWPFTable t : tList) {
            rList = t.getRows();//from   ww  w  .  jav  a2  s. co  m
            rCount = 0;
            cCount = 0;
            rcpCount = 0;
            System.out.println("Table Nr." + (tCount++));
            for (XWPFTableRow r : rList) {
                cList = r.getTableCells();
                cCount = 0;
                rcpCount = 0;
                System.out.println("Row Nr." + (rCount++));
                for (XWPFTableCell c : cList) {
                    rcpList = c.getParagraphs();
                    rcpCount = 0;
                    System.out.println("Cell Nr." + (cCount++));
                    System.out.println("Cell Text: " + c.getText());
                    System.out.println("Nr of Tables: " + c.getTables().size());
                    for (XWPFParagraph rcp : rcpList) {
                        System.out.println("Par Nr." + (rcpCount++) + " Paragraphtext=" + rcp.getText());
                    }
                    for (XWPFTable t1 : c.getTables()) {
                        for (XWPFTableRow r1 : t1.getRows()) {
                            for (XWPFTableCell c1 : r1.getTableCells()) {
                                System.out.println("DC Nr." + dummCounter + " Cell Text: " + c1.getText());
                                switch (dummCounter) {
                                case 0:
                                    wordReference.kundenLogo = c1.getText();
                                    break;
                                case 1:
                                    wordReference.kundenprofil = c1.getText();
                                    break;
                                case 2:
                                    wordReference.ausgangslage = c1.getText();
                                    break;
                                case 3:
                                    wordReference.losung = c1.getText();
                                    break;
                                case 4:
                                    wordReference.ergebnis = c1.getText();
                                    break;
                                case 5:
                                    wordReference.kunde = c1.getText();
                                    break;
                                case 6:
                                    wordReference.projektname = c1.getText();
                                    break;
                                case 7:
                                    wordReference.kundenstatement = c1.getText();
                                    break;
                                case 8:
                                    wordReference.statementBei = c1.getText();
                                    break;
                                case 9:
                                    wordReference.flisstext = c1.getText();
                                    break;
                                default:
                                    ;
                                }
                                dummCounter++;
                            }
                        }

                    }
                }
            }
        }
        System.out.println(wordReference.toString(1));

    }
}

From source file:myexamples.WordDocsExamples.Test1.java

public static WordReference getWordReference() throws IOException {
    WordReference wordReference = new WordReference();
    JFileChooser chooser = new JFileChooser();
    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        System.out.println(chooser.getSelectedFile().getName());
        FileInputStream fis = new FileInputStream(chooser.getSelectedFile());
        XWPFDocument doc = new XWPFDocument(fis);
        XWPFWordExtractor extract = new XWPFWordExtractor(doc);
        //System.out.println(extract.getText());
        List<XWPFParagraph> pList = doc.getParagraphs();
        List<XWPFTable> tList = doc.getTables();

        //            System.out.println("Number of Paragraphs="+pList.size());
        //            System.out.println("Number of Tables="+tList.size());
        List<XWPFTableRow> rList;
        List<XWPFTableCell> cList;
        List<XWPFParagraph> rcpList;
        int tCount = 0, rCount = 0, cCount = 0, rcpCount = 0, dummCounter = 0;

        for (XWPFTable t : tList) {
            rList = t.getRows();//  ww  w  .  j av a2  s.c o m
            rCount = 0;
            cCount = 0;
            rcpCount = 0;
            //               System.out.println("Table Nr."+(tCount++));
            for (XWPFTableRow r : rList) {
                cList = r.getTableCells();
                cCount = 0;
                rcpCount = 0;
                //                System.out.println("Row Nr."+(rCount++));
                for (XWPFTableCell c : cList) {
                    rcpList = c.getParagraphs();
                    rcpCount = 0;
                    //                       System.out.println("Cell Nr."+(cCount++));
                    //                       System.out.println("Cell Text: "+c.getText());
                    //                       System.out.println("Nr of Tables: "+c.getTables().size());
                    for (XWPFParagraph rcp : rcpList) {
                        // System.out.println("Par Nr."+(rcpCount++)+" Paragraphtext="+ rcp.getText());
                    }
                    for (XWPFTable t1 : c.getTables()) {
                        for (XWPFTableRow r1 : t1.getRows()) {
                            for (XWPFTableCell c1 : r1.getTableCells()) {
                                //System.out.println("DC Nr."+dummCounter+" Cell Text: "+c1.getText());
                                switch (dummCounter) {
                                case 0:
                                    wordReference.kundenLogo = c1.getText();
                                    break;
                                case 1:
                                    wordReference.kundenprofil = c1.getText();
                                    break;
                                case 2:
                                    wordReference.ausgangslage = c1.getText();
                                    break;
                                case 3:
                                    wordReference.losung = c1.getText();
                                    break;
                                case 4:
                                    wordReference.ergebnis = c1.getText();
                                    break;
                                case 5:
                                    wordReference.kunde = c1.getText();
                                    break;
                                case 6:
                                    wordReference.projektname = c1.getText();
                                    break;
                                case 7:
                                    wordReference.kundenstatement = c1.getText();
                                    break;
                                case 8:
                                    wordReference.statementBei = c1.getText();
                                    break;
                                case 9:
                                    wordReference.flisstext = c1.getText();
                                    break;
                                default:
                                    ;
                                }
                                dummCounter++;
                            }
                        }

                    }
                }
            }
        }
        //System.out.println(wordReference.toString(1));

    }
    return wordReference;
}

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

/**
 * Inserts the given {@link MTable}./*from   w ww . j a  va 2 s. com*/
 * 
 * @param run
 *            the {@link XWPFRun} to insert to
 * @param table
 *            the {@link MTable} to insert
 */
private void insertMTable(XWPFRun run, MTable table) {
    final XWPFTable docTable;
    if (generatedDocument instanceof XWPFDocument) {
        if (table.getLabel() != null) {
            XWPFRun captionRun;
            captionRun = run;
            IRunBody runBody = captionRun.getParent();
            if (runBody instanceof XWPFParagraph) {
                ((XWPFParagraph) runBody).setSpacingAfter(0);
            }
            captionRun.setText(table.getLabel());
            captionRun.setBold(true);
        }
        docTable = ((XWPFDocument) generatedDocument).createTable();
    } else if (generatedDocument instanceof XWPFHeaderFooter) {
        final XWPFHeaderFooter headerFooter = (XWPFHeaderFooter) generatedDocument;
        final int index = headerFooter._getHdrFtr().getTblArray().length;
        final CTTbl cttbl = headerFooter._getHdrFtr().insertNewTbl(index);
        docTable = new XWPFTable(cttbl, headerFooter);
        headerFooter.insertTable(index, docTable);
    } else if (generatedDocument instanceof XWPFTableCell) {
        XWPFTableCell tcell = (XWPFTableCell) generatedDocument;
        if (table.getLabel() != null) {
            final XWPFRun captionRun = run;
            IRunBody runBody = captionRun.getParent();
            if (runBody instanceof XWPFParagraph) {
                ((XWPFParagraph) runBody).setSpacingAfter(0);
            }
            captionRun.setText(table.getLabel());
            captionRun.setBold(true);
        }
        CTTbl ctTbl = tcell.getCTTc().addNewTbl();
        docTable = new XWPFTable(ctTbl, tcell);
        int tableRank = tcell.getTables().size();
        tcell.insertTable(tableRank, docTable);
        // A paragraph is mandatory at the end of a cell, so let's always add one.
        tcell.addParagraph();
    } else {
        docTable = null;
        M2DocUtils.appendMessageRun((XWPFParagraph) run.getParent(), ValidationMessageLevel.ERROR,
                "m:table can't be inserted here.");
    }

    if (docTable != null) {
        fillTable(docTable, table);
    }
}

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

@Override
public IConstruct caseTable(Table table) {
    // Create the table structure in the destination document.

    CTTbl copy = (CTTbl) table.getTable().getCTTbl().copy();
    copy.getTrList().clear();/* w  w w  .  j  av  a 2 s. com*/
    if (generatedDocument instanceof XWPFDocument) {
        currentGeneratedTable = ((XWPFDocument) generatedDocument).createTable();
        if (currentGeneratedTable.getRows().size() > 0) {
            currentGeneratedTable.removeRow(0);
        }
        currentGeneratedTable.getCTTbl().set(copy);
    } else if (generatedDocument instanceof XWPFHeaderFooter) {
        final XWPFHeaderFooter headerFooter = (XWPFHeaderFooter) generatedDocument;
        final int index = headerFooter._getHdrFtr().getTblArray().length;
        final CTTbl cttbl = headerFooter._getHdrFtr().insertNewTbl(index);
        XWPFTable newTable = new XWPFTable(cttbl, headerFooter);
        if (newTable.getRows().size() > 0) {
            newTable.removeRow(0);
        }
        headerFooter.insertTable(index, newTable);
        currentGeneratedTable = headerFooter.getTables().get(index);
        currentGeneratedTable.getCTTbl().set(copy);
    } else if (generatedDocument instanceof XWPFTableCell) {
        XWPFTableCell tCell = (XWPFTableCell) generatedDocument;
        int tableRank = tCell.getTables().size();
        XWPFTable newTable = new XWPFTable(copy, tCell, 0, 0);
        if (newTable.getRows().size() > 0) {
            newTable.removeRow(0);
        }
        tCell.insertTable(tableRank, newTable);
        currentGeneratedTable = tCell.getTables().get(tableRank);
    } else {
        throw new UnsupportedOperationException("unknown type of IBody : " + generatedDocument.getClass());
    }
    // iterate on the row
    for (Row row : table.getRows()) {
        doSwitch(row);
    }

    return table;
}

From source file:org.obeonetwork.m2doc.generator.TableClientProcessor.java

License:Open Source License

/**
 * Create a table in a table cell./*from ww w .ja  v  a2 s  .  c o  m*/
 * 
 * @param tableRun
 *            The table run
 * @param first
 *            whether it's the 1st table created in this cell
 * @param mtable
 *            The table description
 * @param tcell
 *            The cell in which to create a new table
 * @return The newly creatted table, located in the given cell.
 */
private XWPFTable createTableInCell(XWPFRun tableRun, boolean first, MTable mtable, XWPFTableCell tcell) {
    XWPFTable table;
    if (showTitle() && mtable.getLabel() != null) {
        XWPFRun captionRun;
        if (first) {
            captionRun = tableRun;
            IRunBody runBody = captionRun.getParent();
            if (runBody instanceof XWPFParagraph) {
                ((XWPFParagraph) runBody).setSpacingAfter(0);
            }
        } else {
            XWPFParagraph captionParagraph = tcell.addParagraph();
            captionParagraph.setSpacingAfter(0);
            captionRun = captionParagraph.createRun();
        }
        captionRun.setText(mtable.getLabel());
        captionRun.setBold(true);
    }
    CTTbl ctTbl = tcell.getCTTc().addNewTbl();
    table = new XWPFTable(ctTbl, tcell);
    int tableRank = tcell.getTables().size();
    tcell.insertTable(tableRank, table);
    // A paragraph is mandatory at the end of a cell, so let's always add one.
    tcell.addParagraph();
    return table;
}

From source file:org.obeonetwork.m2doc.generator.TemplateProcessor.java

License:Open Source License

@Override
public AbstractConstruct caseTable(Table object) {
    // Create the table structure in the destination document.
    XWPFTable generatedTable;/*from  w  ww .  j a v a  2 s. c  o m*/
    CTTbl copy = (CTTbl) object.getTable().getCTTbl().copy();
    copy.getTrList().clear();
    if (generatedDocument instanceof XWPFDocument) {
        generatedTable = ((XWPFDocument) generatedDocument).createTable();
        if (generatedTable.getRows().size() > 0) {
            generatedTable.removeRow(0);
        }
        generatedTable.getCTTbl().set(copy);
    } else if (generatedDocument instanceof XWPFTableCell) {
        XWPFTableCell tCell = (XWPFTableCell) generatedDocument;
        int tableRank = tCell.getTables().size();
        XWPFTable newTable = new XWPFTable(copy, tCell, 0, 0);
        if (newTable.getRows().size() > 0) {
            newTable.removeRow(0);
        }
        tCell.insertTable(tableRank, newTable);
        generatedTable = tCell.getTables().get(tableRank);
    } else {
        throw new UnsupportedOperationException("unknown type of IBody : " + generatedDocument.getClass());
    }
    // iterate on the row
    for (Row row : object.getRows()) {
        XWPFTableRow newRow = generatedTable.createRow();
        CTRow ctRow = (CTRow) row.getTableRow().getCtRow().copy();
        ctRow.getTcList().clear();
        newRow.getCtRow().set(ctRow);
        // iterate on cells.
        for (Cell cell : row.getCells()) {
            XWPFTableCell newCell = newRow.createCell();
            CTTc ctCell = (CTTc) cell.getTableCell().getCTTc().copy();
            ctCell.getPList().clear();
            ctCell.getTblList().clear();
            newCell.getCTTc().set(ctCell);
            // process the cell :
            TemplateProcessor processor = new TemplateProcessor(definitions, queryEnvironment, newCell);
            processor.doSwitch(cell.getTemplate());
        }
    }
    return super.caseTable(object);
}

From source file:org.obeonetwork.m2doc.generator.UserContentRawCopy.java

License:Open Source License

/**
 * Create new Table./*from   w ww .j  a  v  a 2 s.co m*/
 * TODO OHA fix bug in nested table on usercontent (else case in code)
 * 
 * @param document
 *            document
 * @param inputTable
 *            input Table
 * @return get Table
 */
private XWPFTable createNewTable(IBody document, XWPFTable inputTable) {
    XWPFTable generatedTable;
    CTTbl copy = (CTTbl) inputTable.getCTTbl().copy();
    if (document instanceof XWPFDocument) {
        generatedTable = ((XWPFDocument) document).createTable();
    } else if (document instanceof XWPFTableCell) {
        XWPFTableCell tCell = (XWPFTableCell) document;
        int tableRank = tCell.getTables().size();
        generatedTable = new XWPFTable(copy, tCell, 0, 0);
        tCell.insertTable(tableRank, generatedTable);
        generatedTable = tCell.getTables().get(tableRank);
    } else {
        throw new UnsupportedOperationException("unknown type of IBody : " + document.getClass());
    }
    return generatedTable;
}