Example usage for org.apache.poi.xwpf.usermodel XWPFTable addRow

List of usage examples for org.apache.poi.xwpf.usermodel XWPFTable addRow

Introduction

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

Prototype

public void addRow(XWPFTableRow row) 

Source Link

Document

add a new Row to the table

Usage

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;/*from  w w w. j av  a 2  s  .com*/
    String propertyName = null;
    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);
                    }
                }
            }
        }
    }
}