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

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

Introduction

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

Prototype

public List<XWPFParagraph> getParagraphs() 

Source Link

Document

returns a list of paragraphs

Usage

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.
        //from  w  w w  . jav a2 s.  c om
 * @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 replaceCellTextStartWithIndex(ExtendedWordDocument xWord, XWPFTableRow rowToDuplicate,
        XWPFTableRow newRow, String propertyName, int index) {
    List<XWPFTableCell> tableCells = rowToDuplicate.getTableCells();
    for (int c = 0; c < tableCells.size(); c++) {
        XWPFTableCell tarCell = newRow.getCell(c);

        for (XWPFParagraph para : tarCell.getParagraphs()) {
            boolean didReplace = true;
            while (didReplace) {
                didReplace = xWord.replaceInParagraph(para, propertyName + "[*]",
                        propertyName + "[" + index + "]");
            }//  w w w.  j av a2 s .co  m
            fillParagraph(xWord, para);
        }
    }
}

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  ww w  .jav a2s  .co m
    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);
                    }
                }
            }
        }
    }
}

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 ww.j av a  2 s  .  co m*/
 * @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:de.knowwe.include.export.CellBuilder.java

License:Open Source License

public CellBuilder(DocumentBuilder decorate, XWPFTableCell cell, boolean isHeader) {
    super(decorate.getModel());
    this.cell = cell;
    this.isHeader = isHeader;

    // prepare paragraph and style
    decorate.closeParagraph();//w w w  .  j a  va 2s  .  c o m
    this.paragraph = cell.getParagraphs().get(0);
    this.paragraph.setStyle(getDefaultStyle().getStyleName());
}

From source file:DocxProcess.DocxTemplateReplacer.java

public void replace(Map<String, String> data) {

    for (XWPFParagraph p : docx.getParagraphs()) {
        replaceParagraph(p, data);//from  ww  w .ja v  a 2  s  .  c  o  m
    }
    for (XWPFTable tbl : docx.getTables()) {
        for (XWPFTableRow row : tbl.getRows()) {
            for (XWPFTableCell cell : row.getTableCells()) {
                for (XWPFParagraph p : cell.getParagraphs()) {
                    replaceParagraph(p, data);
                }
            }
        }
    }

}

From source file:edu.gatech.pmase.capstone.awesome.impl.output.DisasterResponseTradeStudyOutputer.java

License:Open Source License

/**
 * Creates the architecture attribute description cell.
 *
 * @param detailsCell the cell to create the description in
 * @param attrs the attributes to put into the description
 *///ww w  . jav  a2  s.co  m
private void createArchitectureAttributeDescription(final XWPFTableCell detailsCell,
        final List<ArchitectureOptionAttribute> attrs) {

    for (int x = 0; x < attrs.size(); x++) {
        final ArchitectureOptionAttribute attr = attrs.get(x);
        LOGGER.debug("Creating architecture option description for attribute: " + attr.getLabel());

        final XWPFParagraph para;
        if (x == 0) {
            para = detailsCell.getParagraphs().get(0);
        } else {
            para = detailsCell.addParagraph();
        }

        final XWPFRun rh = para.createRun();
        rh.setText(DisasterResponseTradeStudyOutputer.createAttribtueString(attr));
        para.setAlignment(ParagraphAlignment.CENTER);
    }
}

From source file:edu.gatech.pmase.capstone.awesome.impl.output.DisasterResponseTradeStudyOutputer.java

License:Open Source License

/**
 * Creates output for architecture options if no architecture was found.
 *
 * @param label the cell with the label//from ww w .ja  va 2 s .c o m
 * @param optionName the name of the option to place
 */
private static void createNoOptionText(final XWPFTableCell label, final String optionName) {
    LOGGER.debug("Creationg No Option Text for option: " + optionName);
    final XWPFParagraph platPara = label.getParagraphs().get(0);
    final XWPFRun rh = platPara.createRun();
    rh.setColor(DisasterResponseTradeStudyOutputer.NO_OPT_TEXT_COLOR);
    rh.setText("No " + optionName + " Satisfies Selections");
    platPara.setAlignment(ParagraphAlignment.CENTER);
}

From source file:export.TableFunctionalReq.java

protected static void createReqFuncTable(XWPFDocument doc, FunctionalRequirement funcReq) {

    int[] cols = { 2943, 6507 };

    XWPFTable rf = doc.createTable(9, 2);

    // Get a list of the rows in the table
    List<XWPFTableRow> rows = rf.getRows();
    int rowCt = 0;
    int colCt = 0;

    for (XWPFTableRow row : rows) {

        // get the cells in this row
        List<XWPFTableCell> cells = row.getTableCells();
        for (XWPFTableCell cell : cells) {
            // get a table cell properties element (tcPr)
            CTTcPr tcpr = cell.getCTTc().addNewTcPr();

            // create cell color element
            CTShd ctshd = tcpr.addNewShd();

            ctshd.setColor("auto");
            ctshd.setVal(STShd.CLEAR);//from www  .java 2 s .  c o  m

            if (colCt == 0) {
                ctshd.setFill("5C7F92");
            }

            // get 1st paragraph in cell's paragraph list
            XWPFParagraph para = cell.getParagraphs().get(0);
            para.setStyle("AltranNormal");
            para.setSpacingAfter(120);
            para.setSpacingBefore(120);

            // create a run to contain the content
            XWPFRun rh = para.createRun();
            //rh.setFontSize(11);

            rh.setFontFamily("Lucida Sans Unicode");

            if (colCt == 0) {
                rh.setColor("FFFFFF");
            }

            if (rowCt == 0 && colCt == 0) {
                rh.setText("RF " + ((x < 9) ? "0" + x : x) + "- F");
                x++;
            } else if (rowCt == 1 && colCt == 0) {
                rh.setText("Use Case (se disponvel):");
            } else if (rowCt == 2 && colCt == 0) {
                rh.setText("Descrio:");
            } else if (rowCt == 3 && colCt == 0) {
                rh.setText("Fonte:");
            } else if (rowCt == 4 && colCt == 0) {
                rh.setText("Fundamento:");
            } else if (rowCt == 5 && colCt == 0) {
                rh.setText("Critrio de avaliao:");
            } else if (rowCt == 6 && colCt == 0) {
                rh.setText("Satisfao do cliente:");
            } else if (rowCt == 7 && colCt == 0) {
                rh.setText("Insatisfao do cliente:");
            } else if (rowCt == 8 && colCt == 0) {
                rh.setText("Histrico:");
            }

            if (rowCt == 0 && colCt == 1) {// Nome do requisito
                rh.setText(funcReq.getName());
                rh.setBold(true);
            } else if (rowCt == 1 && colCt == 1) { // UseCases
                String testUC = "";
                int cntUC = 0;
                for (UseCase uc : funcReq.getUseCaseCollection()) {

                    if (cntUC == 0) {
                        testUC = uc.getName();
                    } else {
                        testUC = testUC + ", " + uc.getName();
                    }
                    cntUC++;
                }

                rh.setText(testUC);
            } else if (rowCt == 2 && colCt == 1) {// Descrio
                rh.setText(funcReq.getDescription());
            } else if (rowCt == 3 && colCt == 1) {// Fonte
                rh.setText(funcReq.getSource());
            } else if (rowCt == 4 && colCt == 1) {// Fundamento
                rh.setText(funcReq.getReason());
            } else if (rowCt == 5 && colCt == 1) {// Criterio de Avalicao
                rh.setText(funcReq.getAvaliationCriteria());
            } else if (rowCt == 6 && colCt == 1) {// Prioridade
                rh.setText(funcReq.getClientPriority().toString());
            } else if (rowCt == 7 && colCt == 1) {// Insatisfao
                rh.setText(funcReq.getClientInsatisfaction().toString());
            } else if (rowCt == 8 && colCt == 1) {// Historico
                rh.setText("Histrico");
            }

            cell.getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(cols[colCt]));
            colCt++;
        }
        colCt = 0;
        rowCt++;

    }

    doc.createParagraph().createRun().addBreak();

}

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

License:Open Source License

private void testTableCell(XWPFTableCell cell, XWPFStylesDocument stylesDocument) {
    XWPFParagraph paragraph = cell.getParagraphs().get(0);
    if ("A".equals(paragraph.getText())) {
        testsA(paragraph, stylesDocument);
    } else if ("B".equals(paragraph.getText())) {
        testsB(paragraph, stylesDocument);
    } else if ("C".equals(paragraph.getText())) {
        testsC(paragraph, stylesDocument);
    } else if ("D".equals(paragraph.getText())) {
        testsD(paragraph, stylesDocument);
    } else if ("E".equals(paragraph.getText())) {
        testsE(paragraph, stylesDocument);
    } else if ("F".equals(paragraph.getText())) {
        testsF(paragraph, stylesDocument);
    } else if ("G".equals(paragraph.getText())) {
        testsG(paragraph, stylesDocument);
    } else if ("H".equals(paragraph.getText())) {
        testsH(paragraph, stylesDocument);
    } else if ("I".equals(paragraph.getText())) {
        testsI(paragraph, stylesDocument);
    }// w w w  .  j  av  a2 s  .c  o m
}