Example usage for org.apache.poi.xwpf.usermodel XWPFTableRow getCtRow

List of usage examples for org.apache.poi.xwpf.usermodel XWPFTableRow getCtRow

Introduction

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

Prototype

@Internal
    public CTRow getCtRow() 

Source Link

Usage

From source file:br.com.techne.gluonsoft.eowexport.builder.WordBuilder.java

License:Apache License

/**
 * adiciona linha  tabela/*  w w  w. j a v  a 2  s  .  c  o  m*/
 * @param rows
 * @param data
 * @param indexRow
 * @param vcutil
 * @param columnIndex
 */
private static void addRow(List<XWPFTableRow> rows, HashMap<String, Object> data, int indexRow,
        ValueCellUtil vcutil, String[] columnIndex) {
    //adicionando titulo
    XWPFTableRow row = rows.get(indexRow);
    // get table row properties (trPr)
    CTTrPr trPr = row.getCtRow().addNewTrPr();
    // set row height; units = twentieth of a point, 360 = 0.25"
    CTHeight ht = trPr.addNewTrHeight();
    ht.setVal(BigInteger.valueOf(360));
    // get the cells in this row
    List<XWPFTableCell> cells = row.getTableCells();

    List<String> keysAttribs = null;
    if (columnIndex.length == 0) {
        keysAttribs = Arrays.asList(data.keySet().toArray(new String[0]));
        Collections.reverse(keysAttribs);
    } else {
        keysAttribs = Arrays.asList(columnIndex);
    }

    //List<String> keysAttribs = Arrays.asList(data.keySet().toArray(new String[0]));
    //Collections.reverse(keysAttribs);
    int colCt = 0;//counter cells

    // add content to each cell
    for (XWPFTableCell cell : cells) {
        // get a table cell properties element (tcPr)
        CTTcPr tcpr = cell.getCTTc().addNewTcPr();
        // set vertical alignment to "center"
        CTVerticalJc va = tcpr.addNewVAlign();
        va.setVal(STVerticalJc.CENTER);

        // create cell color element
        CTShd ctshd = tcpr.addNewShd();
        ctshd.setColor("auto");
        ctshd.setVal(STShd.CLEAR);

        //zebrando tabela
        if (indexRow % 2 == 0) {
            // even row
            ctshd.setFill("D3DFEE");
        } else {
            // odd row
            ctshd.setFill("EDF2F8");
        }

        // get 1st paragraph in cell's paragraph list
        XWPFParagraph para = cell.getParagraphs().get(0);
        // create a run to contain the content
        XWPFRun rh = para.createRun();

        if (data.get(keysAttribs.get(colCt)) == null) {
            data.put(keysAttribs.get(colCt), "");
        } else {
            // other rows
            rh.setText(vcutil.parseValue(data.get(keysAttribs.get(colCt))).toString());
            para.setAlignment(ParagraphAlignment.LEFT);
        }

        colCt++;
    } // for cell
}

From source file:br.com.techne.gluonsoft.eowexport.builder.WordBuilder.java

License:Apache License

/**
 * adiciona titulo/*from  w  w  w .  j a v a2  s  .  c  o m*/
 * @param rows
 * @param titles
 */
private static void addTitle(List<XWPFTableRow> rows, String[] titles) {
    //adicionando titulo
    XWPFTableRow row = rows.get(0);
    // get table row properties (trPr)
    CTTrPr trPr = row.getCtRow().addNewTrPr();
    // set row height; units = twentieth of a point, 360 = 0.25"
    CTHeight ht = trPr.addNewTrHeight();
    ht.setVal(BigInteger.valueOf(360));
    // get the cells in this row
    List<XWPFTableCell> cells = row.getTableCells();
    int colCt = 0;

    //CTTblWidth tblWidth = CTTblWidth.Factory.newInstance();
    //tblWidth.setW(BigInteger.valueOf(200));
    //tblWidth.setType(STTblWidth.DXA);

    // add content to each cell
    for (XWPFTableCell cell : cells) {
        // get a table cell properties element (tcPr)
        CTTcPr tcpr = cell.getCTTc().addNewTcPr();
        //tcpr.setTcW(tblWidth);

        // set vertical alignment to "center"
        CTVerticalJc va = tcpr.addNewVAlign();
        va.setVal(STVerticalJc.CENTER);

        // create cell color element
        CTShd ctshd = tcpr.addNewShd();
        ctshd.setColor("auto");
        ctshd.setVal(STShd.CLEAR);
        // header row
        ctshd.setFill("A7BFDE");
        // get 1st paragraph in cell's paragraph list
        XWPFParagraph para = cell.getParagraphs().get(0);
        // create a run to contain the content
        XWPFRun rh = para.createRun();

        // header row
        if ((titles.length - 1) < colCt) {
            rh.setText("");
        } else
            rh.setText(titles[colCt]);

        rh.setBold(true);
        para.setAlignment(ParagraphAlignment.CENTER);

        colCt++;
    } // for cell
}

From source file:com.deepoove.poi.tl.SimpleTable.java

License:Apache License

/**
 * Create a table with some row and column styling. I "manually" add the
 * style name to the table, but don't check to see if the style actually
 * exists in the document. Since I'm creating it from scratch, it obviously
 * won't exist. When opened in MS Word, the table style becomes "Normal".
 * I manually set alternating row colors. This could be done using Themes,
 * but that's left as an exercise for the reader. The cells in the last
 * column of the table have 10pt. "Courier" font.
 * I make no claims that this is the "right" way to do it, but it worked
 * for me. Given the scarcity of XWPF examples, I thought this may prove
 * instructive and give you ideas for your own solutions.
        // ww w. java 2s.  c o m
 * @throws Exception
 */
public static void createStyledTable() throws Exception {
    // Create a new document from scratch
    XWPFDocument doc = new XWPFDocument();
    // -- OR --
    // open an existing empty document with styles already defined
    //XWPFDocument doc = new XWPFDocument(new FileInputStream("base_document.docx"));

    // Create a new table with 6 rows and 3 columns
    int nRows = 6;
    int nCols = 3;
    XWPFTable table = doc.createTable(nRows, nCols);

    // Set the table style. If the style is not defined, the table style
    // will become "Normal".
    CTTblPr tblPr = table.getCTTbl().getTblPr();
    CTString styleStr = tblPr.addNewTblStyle();
    styleStr.setVal("StyledTable");

    // Get a list of the rows in the table
    List<XWPFTableRow> rows = table.getRows();
    int rowCt = 0;
    int colCt = 0;
    for (XWPFTableRow row : rows) {
        // get table row properties (trPr)
        CTTrPr trPr = row.getCtRow().addNewTrPr();
        // set row height; units = twentieth of a point, 360 = 0.25"
        CTHeight ht = trPr.addNewTrHeight();
        ht.setVal(BigInteger.valueOf(360));

        // get the cells in this row
        List<XWPFTableCell> cells = row.getTableCells();
        // add content to each cell
        for (XWPFTableCell cell : cells) {
            // get a table cell properties element (tcPr)
            CTTcPr tcpr = cell.getCTTc().addNewTcPr();
            // set vertical alignment to "center"
            CTVerticalJc va = tcpr.addNewVAlign();
            va.setVal(STVerticalJc.CENTER);

            // create cell color element
            CTShd ctshd = tcpr.addNewShd();
            ctshd.setColor("auto");
            ctshd.setVal(STShd.CLEAR);
            if (rowCt == 0) {
                // header row
                ctshd.setFill("A7BFDE");
            } else if (rowCt % 2 == 0) {
                // even row
                ctshd.setFill("D3DFEE");
            } else {
                // odd row
                ctshd.setFill("EDF2F8");
            }

            // get 1st paragraph in cell's paragraph list
            XWPFParagraph para = cell.getParagraphs().get(0);
            // create a run to contain the content
            XWPFRun rh = para.createRun();
            // style cell as desired
            if (colCt == nCols - 1) {
                // last column is 10pt Courier
                rh.setFontSize(10);
                rh.setFontFamily("Courier");
            }
            if (rowCt == 0) {
                // header row
                rh.setText("header row, col " + colCt);
                rh.setBold(true);
                para.setAlignment(ParagraphAlignment.CENTER);
            } else if (rowCt % 2 == 0) {
                // even row
                rh.setText("row " + rowCt + ", col " + colCt);
                para.setAlignment(ParagraphAlignment.LEFT);
            } else {
                // odd row
                rh.setText("row " + rowCt + ", col " + colCt);
                para.setAlignment(ParagraphAlignment.LEFT);
            }
            colCt++;
        } // for cell
        colCt = 0;
        rowCt++;
    } // for row

    // write the file
    FileOutputStream out = new FileOutputStream("styledTable.docx");
    doc.write(out);
    out.close();
}

From source file:com.foc.msword.WordTemplateFillerResource.java

License:Apache License

private void fillTable(ExtendedWordDocument xWord, XWPFTable xwpfTable) {
    List<XWPFTableRow> tableRows = xwpfTable.getRows();

    XWPFTableRow rowToDuplicate = null;
    String propertyName = null;//from  w w  w.j a v a 2  s  . c o m
    FocList slaveList = null;

    //First we check if this row is to be duplicated [*]
    for (int r = 0; r < tableRows.size() && slaveList == null; r++) {
        XWPFTableRow xwpfTableRow = tableRows.get(r);

        List<XWPFTableCell> tableCells = xwpfTableRow.getTableCells();
        for (XWPFTableCell xwpfTableCell : tableCells) {
            List<XWPFParagraph> paragraphs = xwpfTableCell.getParagraphs();
            if (paragraphs != null) {
                for (int p = 0; p < paragraphs.size() && slaveList == null; p++) {
                    XWPFParagraph para = paragraphs.get(p);
                    String paragraphText = para.getParagraphText();
                    int listStartIndex = paragraphText.indexOf("_LIST[*].");
                    int dollarIndex = paragraphText.indexOf("$F{");
                    if (dollarIndex >= 0 && listStartIndex > dollarIndex) {
                        propertyName = paragraphText.substring(dollarIndex + 3, listStartIndex + 5);
                        Object res = getFocData().iFocData_getDataByPath(propertyName);
                        if (res instanceof FocList) {
                            slaveList = (FocList) res;
                            rowToDuplicate = xwpfTableRow;
                        }
                    }
                }
            }
        }
    }

    if (slaveList != null) {
        for (int i = 1; i < slaveList.size(); i++) {
            // Copying a existing table row? 
            CTRow ctRow = CTRow.Factory.newInstance();
            ctRow.set(rowToDuplicate.getCtRow());
            XWPFTableRow newRow = new XWPFTableRow(ctRow, xwpfTable);

            replaceCellTextStartWithIndex(xWord, rowToDuplicate, newRow, propertyName, i);

            xwpfTable.addRow(newRow);
        }

        replaceCellTextStartWithIndex(xWord, rowToDuplicate, rowToDuplicate, propertyName, 0);

    } else {
        tableRows = xwpfTable.getRows();
        for (int r = 0; r < tableRows.size(); r++) {
            XWPFTableRow xwpfTableRow = tableRows.get(r);
            List<XWPFTableCell> tableCells = xwpfTableRow.getTableCells();
            for (XWPFTableCell xwpfTableCell : tableCells) {
                List<XWPFParagraph> paragraphs = xwpfTableCell.getParagraphs();
                if (paragraphs != null) {
                    for (XWPFParagraph para : paragraphs) {
                        fillParagraph(xWord, para);
                    }
                }
            }
        }
    }
}

From source file:com.glodon.tika.SimpleTable.java

License:Apache License

/**
 * Create a table with some row and column styling. I "manually" add the
 * style name to the table, but don't check to see if the style actually
 * exists in the document. Since I'm creating it from scratch, it obviously
 * won't exist. When opened in MS Word, the table style becomes "Normal".
 * I manually set alternating row colors. This could be done using Themes,
 * but that's left as an exercise for the reader. The cells in the last
 * column of the table have 10pt. "Courier" font.
 * I make no claims that this is the "right" way to do it, but it worked
 * for me. Given the scarcity of XWPF examples, I thought this may prove
 * instructive and give you ideas for your own solutions.
        //w w w  .ja v a 2  s .c om
 * @throws Exception
 */
public static void createStyledTable() throws Exception {
    // Create a new document from scratch
    XWPFDocument doc = new XWPFDocument();

    try {
        // -- OR --
        // open an existing empty document with styles already defined
        //XWPFDocument doc = new XWPFDocument(new FileInputStream("base_document.docx"));

        // Create a new table with 6 rows and 3 columns
        int nRows = 6;
        int nCols = 3;
        XWPFTable table = doc.createTable(nRows, nCols);

        // Set the table style. If the style is not defined, the table style
        // will become "Normal".
        CTTblPr tblPr = table.getCTTbl().getTblPr();
        CTString styleStr = tblPr.addNewTblStyle();
        styleStr.setVal("StyledTable");

        // Get a list of the rows in the table
        List<XWPFTableRow> rows = table.getRows();
        int rowCt = 0;
        int colCt = 0;
        for (XWPFTableRow row : rows) {
            // get table row properties (trPr)
            CTTrPr trPr = row.getCtRow().addNewTrPr();
            // set row height; units = twentieth of a point, 360 = 0.25"
            CTHeight ht = trPr.addNewTrHeight();
            ht.setVal(BigInteger.valueOf(360));

            // get the cells in this row
            List<XWPFTableCell> cells = row.getTableCells();
            // add content to each cell
            for (XWPFTableCell cell : cells) {
                // get a table cell properties element (tcPr)
                CTTcPr tcpr = cell.getCTTc().addNewTcPr();
                // set vertical alignment to "center"
                CTVerticalJc va = tcpr.addNewVAlign();
                va.setVal(STVerticalJc.CENTER);

                // create cell color element
                CTShd ctshd = tcpr.addNewShd();
                ctshd.setColor("auto");
                ctshd.setVal(STShd.CLEAR);
                if (rowCt == 0) {
                    // header row
                    ctshd.setFill("A7BFDE");
                } else if (rowCt % 2 == 0) {
                    // even row
                    ctshd.setFill("D3DFEE");
                } else {
                    // odd row
                    ctshd.setFill("EDF2F8");
                }

                // get 1st paragraph in cell's paragraph list
                XWPFParagraph para = cell.getParagraphs().get(0);
                // create a run to contain the content
                XWPFRun rh = para.createRun();
                // style cell as desired
                if (colCt == nCols - 1) {
                    // last column is 10pt Courier
                    rh.setFontSize(10);
                    rh.setFontFamily("Courier");
                }
                if (rowCt == 0) {
                    // header row
                    rh.setText("header row, col " + colCt);
                    rh.setBold(true);
                    para.setAlignment(ParagraphAlignment.CENTER);
                } else {
                    // other rows
                    rh.setText("row " + rowCt + ", col " + colCt);
                    para.setAlignment(ParagraphAlignment.LEFT);
                }
                colCt++;
            } // for cell
            colCt = 0;
            rowCt++;
        } // for row

        // write the file
        OutputStream out = new FileOutputStream("styledTable.docx");
        try {
            doc.write(out);
        } finally {
            out.close();
        }
    } finally {
        doc.close();
    }
}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.table.row.AbstractTableRowExValueProvider.java

License:Open Source License

public CTTblPrEx getTblPrEx(XWPFTableRow row) {
    return row.getCtRow().getTblPrEx();
}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.table.row.AbstractTableRowValueProvider.java

License:Open Source License

public CTTrPr getTrPr(XWPFTableRow row) {
    return row.getCtRow().getTrPr();
}

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;
    }/*w  w w  .  jav  a 2s  . c  om*/

    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.utils.XWPFTableUtil.java

License:Open Source License

private static int getNbColumnsToIgnore(XWPFTableRow row, boolean before) {
    CTTrPr trPr = row.getCtRow().getTrPr();
    if (trPr == null) {
        return 0;
    }// www .  j a va2  s. co  m

    List<CTDecimalNumber> gridBeforeAfters = before ? trPr.getGridBeforeList() : trPr.getGridAfterList();
    if (gridBeforeAfters == null || gridBeforeAfters.size() < 1) {
        return 0;
    }
    int nbColumns = 0;
    BigInteger val = null;
    for (CTDecimalNumber gridBeforeAfter : gridBeforeAfters) {
        val = gridBeforeAfter.getVal();
        if (val != null) {
            nbColumns += val.intValue();
        }
    }

    return nbColumns;
}

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;/*w  w w  .ja  v a  2  s  .co  m*/
        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);
}