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

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

Introduction

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

Prototype

public XWPFTableCell(CTTc cell, XWPFTableRow tableRow, IBody part) 

Source Link

Document

If a table cell does not include at least one block-level element, then this document shall be considered corrupt

Usage

From source file:fr.opensagres.poi.xwpf.converter.core.styles.TableInfo.java

License:Open Source License

private void computeCellInfos(XWPFTableRow row) {
    if (nbColumns == null) {
        nbColumns = XWPFTableUtil.computeColWidths(row.getTable()).length;
    }/*from  w  w  w  .  j  av a 2  s.  com*/

    int rowIndex = table.getRows().indexOf(row);
    boolean firstRow = rowIndex == 0;
    boolean lastRow = rowIndex == (table.getRows().size() - 1);

    boolean firstCol = true;
    boolean lastCol = false;
    int cellIndex = 0;
    CTRow ctRow = row.getCtRow();
    XmlCursor c = ctRow.newCursor();
    c.selectPath("./*");
    while (c.toNextSelection()) {
        XmlObject o = c.getObject();
        if (o instanceof CTTc) {
            CTTc tc = (CTTc) o;
            XWPFTableCell cell = row.getTableCell(tc);
            cellIndex = getCellIndex(cellIndex, cell);
            lastCol = (cellIndex == nbColumns - 1);
            addCellInfo(cell, firstRow, lastRow, firstCol, lastCol);
            firstCol = false;
        } else if (o instanceof CTSdtCell) {
            // Fix bug of POI
            CTSdtCell sdtCell = (CTSdtCell) o;
            List<CTTc> tcList = sdtCell.getSdtContent().getTcList();
            for (CTTc ctTc : tcList) {
                XWPFTableCell cell = new XWPFTableCell(ctTc, row, row.getTable().getBody());
                cellIndex = getCellIndex(cellIndex, cell);
                lastCol = (cellIndex == nbColumns - 1);
                addCellInfo(row.getTableCell(ctTc), firstRow, lastRow, firstCol, lastCol);
                firstCol = false;
            }
        }
    }
    c.dispose();
}

From source file:fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.java

License:Open Source License

protected void visitTableRow(XWPFTableRow row, float[] colWidths, T tableContainer, boolean firstRow,
        boolean lastRowIfNoneVMerge, int rowIndex, int rowsSize) throws Exception {

    boolean headerRow = stylesDocument.isTableRowHeader(row);
    startVisitTableRow(row, tableContainer, rowIndex, headerRow);

    int nbColumns = colWidths.length;
    // Process cell
    boolean firstCol = true;
    boolean lastCol = false;
    boolean lastRow = false;
    List<XWPFTableCell> vMergedCells = null;
    List<XWPFTableCell> cells = row.getTableCells();
    if (nbColumns > cells.size()) {
        // Columns number is not equal to cells number.
        // POI have a bug with
        // <w:tr w:rsidR="00C55C20">
        // <w:tc>
        // <w:tc>...
        // <w:sdt>
        // <w:sdtContent>
        // <w:tc> <= this tc which is a XWPFTableCell is not included in the row.getTableCells();

        firstCol = true;/*from w  w  w .j  a v a2s. c  om*/
        int cellIndex = -1;
        int cellPtr = 0;
        CTRow ctRow = row.getCtRow();
        XmlCursor c = ctRow.newCursor();
        c.selectPath("./*");
        while (c.toNextSelection()) {
            XmlObject o = c.getObject();
            if (o instanceof CTTc) {
                CTTc tc = (CTTc) o;
                XWPFTableCell cell = row.getTableCell(tc);
                cellIndex = getCellIndex(cellIndex, cell);
                lastCol = (cellIndex == nbColumns);
                vMergedCells = getVMergedCells(cell, rowIndex, cellPtr);
                if (vMergedCells == null || vMergedCells.size() > 0) {
                    lastRow = isLastRow(lastRowIfNoneVMerge, rowIndex, rowsSize, vMergedCells);
                    visitCell(cell, tableContainer, firstRow, lastRow, firstCol, lastCol, rowIndex, cellPtr,
                            vMergedCells);
                }
                cellPtr++;
                firstCol = false;
            } else if (o instanceof CTSdtCell) {
                // Fix bug of POI
                CTSdtCell sdtCell = (CTSdtCell) o;
                List<CTTc> tcList = sdtCell.getSdtContent().getTcList();
                for (CTTc ctTc : tcList) {
                    XWPFTableCell cell = new XWPFTableCell(ctTc, row, row.getTable().getBody());
                    cellIndex = getCellIndex(cellIndex, cell);
                    lastCol = (cellIndex == nbColumns);
                    List<XWPFTableCell> rowCells = row.getTableCells();
                    if (!rowCells.contains(cell)) {
                        rowCells.add(cell);
                    }
                    vMergedCells = getVMergedCells(cell, rowIndex, cellPtr);
                    if (vMergedCells == null || vMergedCells.size() > 0) {
                        lastRow = isLastRow(lastRowIfNoneVMerge, rowIndex, rowsSize, vMergedCells);
                        visitCell(cell, tableContainer, firstRow, lastRow, firstCol, lastCol, rowIndex, cellPtr,
                                vMergedCells);
                    }
                    cellPtr++;
                    firstCol = false;
                }
            }
        }
        c.dispose();
    } else {
        // Column number is equal to cells number.
        for (int i = 0; i < cells.size(); i++) {
            lastCol = (i == cells.size() - 1);
            XWPFTableCell cell = cells.get(i);
            vMergedCells = getVMergedCells(cell, rowIndex, i);
            if (vMergedCells == null || vMergedCells.size() > 0) {
                lastRow = isLastRow(lastRowIfNoneVMerge, rowIndex, rowsSize, vMergedCells);
                visitCell(cell, tableContainer, firstRow, lastRow, firstCol, lastCol, rowIndex, i,
                        vMergedCells);
            }
            firstCol = false;
        }
    }

    endVisitTableRow(row, tableContainer, firstRow, lastRow, headerRow);
}