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

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

Introduction

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

Prototype

public void setRepeatHeader(boolean repeat) 

Source Link

Document

This attribute controls whether to repeat a table's header row at the top of a table split across pages.

Usage

From source file:com.foc.vaadin.gui.layouts.validationLayout.FVValidationLayout.java

License:Apache License

private void createWordTable() {
    try {/*from   w  w w.  jav a2s.c o  m*/
        XWPFDocument document = new XWPFDocument();

        // New 2x2 table
        XWPFTable tableOne = document.createTable();
        XWPFTableRow tableOneRowOne = tableOne.getRow(0);
        tableOneRowOne.setRepeatHeader(true);

        tableOneRowOne.getCell(0).setText("Hello");
        tableOneRowOne.addNewTableCell().setText("World");

        XWPFTableRow tableOneRowTwo = tableOne.createRow();

        tableOneRowTwo.getCell(0).setText("This is");
        tableOneRowTwo.getCell(1).setText("a table");

        for (int i = 1; i < 50; i++) {
            tableOneRowTwo = tableOne.createRow();
            tableOneRowTwo.getCell(0).setText("row " + i + " tol 0 bla bla very long string");
            tableOneRowTwo.getCell(1).setText("row " + i + " col 1");
        }

        //Add a break between the tables
        document.createParagraph().createRun().addBreak();

        // New 3x3 table
        XWPFTable tableTwo = document.createTable();
        XWPFTableRow tableTwoRowOne = tableTwo.getRow(0);
        tableTwoRowOne.getCell(0).setText("col one, row one");
        tableTwoRowOne.addNewTableCell().setText("col two, row one");
        tableTwoRowOne.addNewTableCell().setText("col three, row one");

        XWPFTableRow tableTwoRowTwo = tableTwo.createRow();
        tableTwoRowTwo.getCell(0).setText("col one, row two");
        tableTwoRowTwo.getCell(1).setText("col two, row two");
        tableTwoRowTwo.getCell(2).setText("col three, row two");

        XWPFTableRow tableTwoRowThree = tableTwo.createRow();
        tableTwoRowThree.getCell(0).setText("col one, row three");
        tableTwoRowThree.getCell(1).setText("col two, row three");
        tableTwoRowThree.getCell(2).setText("col three, row three");

        FileOutputStream outStream = new FileOutputStream(
                "C://Users//user//Desktop//POI Word Doc Sample Table 1.docx");

        document.write(outStream);
        outStream.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.foc.vaadin.gui.mswordGenerator.FocMSWordTable.java

License:Apache License

@Override
public void write(IMSWordContainer container) {
    XWPFTable table = container.insertTable();
    IFocData rootFocData = getFocData();
    if (table != null && rootFocData != null && getXmlAttribute() != null) {

        String dataPath = getXmlAttribute().getValue(FXML.ATT_DATA_PATH);

        IFocData focData = rootFocData.iFocData_getDataByPath(dataPath);
        if (focData == null) {
            focData = rootFocData;//from  www  .j a  v  a2  s . c  o m
        }
        if (focData != null) {
            if (focData != null && focData instanceof FocList) {
                FocList focList = (FocList) focData;

                int numberOfRows = focList.size();
                int numberOfColumns = getComponentNumber();

                FocObject focObject = null;

                XWPFTableRow row = table.getRow(0);
                row.setRepeatHeader(true);

                for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
                    FocMSWordTableColumn tableColumn = (FocMSWordTableColumn) getComponentAt(columnIndex);
                    String caption = tableColumn.getXmlAttribute().getValue(FXML.ATT_CAPTION);
                    addCell(row, columnIndex, caption, true);
                }

                for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++) {
                    row = table.createRow();
                    focObject = focList.getFocObject(rowIndex);
                    for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
                        FocMSWordTableColumn tableColumn = (FocMSWordTableColumn) getComponentAt(columnIndex);
                        IFocData data = focObject.iFocData_getDataByPath(tableColumn.getDataPath());
                        if (data != null && data instanceof FProperty) {
                            addCell(row, columnIndex, data.toString());
                        } else {
                            addCell(row, columnIndex, "");
                        }
                    }
                }
            }
        }
    }
}

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 ww  w  .  ja  va  2 s  .  c o  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();
}