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

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

Introduction

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

Prototype

public void setCantSplitRow(boolean split) 

Source Link

Document

Controls whether to allow this table row to split across pages.

Usage

From source file:de.knowwe.include.export.TableExporter.java

License:Open Source License

@Override
public void export(Section<WikiTable> section, DocumentBuilder manager) throws ExportException {
    // initialize table for easier access
    Matrix<Section<TableCell>> matrix = toMatrix(section);

    // create table with correct dimension
    XWPFDocument doc = manager.getDocument();
    XWPFTable table = doc.createTable(matrix.getRowSize(), matrix.getColSize());
    boolean headerCellsOnly = true;

    for (int row = 0; row < matrix.getRowSize(); row++) {
        XWPFTableRow tableRow = table.getRow(row);
        for (int col = 0; col < matrix.getColSize(); col++) {
            Section<TableCell> cell = matrix.get(row, col);
            boolean isHeader = cell.get().isHeader(cell);
            boolean isZebra = row % 2 == 0;
            headerCellsOnly &= isHeader;

            // prepare cell shading
            XWPFTableCell tableCell = tableRow.getCell(col);
            CTShd shade = tableCell.getCTTc().addNewTcPr().addNewShd();
            if (isHeader) {
                shade.setFill("D0D0D0");
                tableCell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
            } else if (isZebra) {
                shade.setFill("F2F2F2");
            }/*from w w w .j  a v a  2  s. co m*/

            // fill cell contents
            DocumentBuilder cellBuilder = new CellBuilder(manager, tableCell, isHeader);
            Section<?> content = Sections.successor(cell, ParagraphTypeForLists.class);
            if (content != null) {
                cellBuilder.export(content);
            }

            // clean trailing white-spaces of each cell
            List<CTR> runs = cellBuilder.getParagraph().getCTP().getRList();
            if (runs.isEmpty())
                continue;
            CTR ctr = runs.get(runs.size() - 1);
            List<CTText> texts = ctr.getTList();
            if (texts.isEmpty())
                continue;
            CTText ctText = texts.get(texts.size() - 1);
            ctText.setStringValue(Strings.trimRight(ctText.getStringValue()));
        }
        // check if the first row(s) have only headers,
        // repeat those headers until a non-header cell has come
        if (headerCellsOnly) {
            tableRow.setRepeatHeader(true);
        }
        tableRow.setCantSplitRow(true);
    }

    // append empty line after each table
    manager.closeParagraph();
    manager.append("\n\r");
    manager.closeParagraph();
}