Java Code Examples for org.apache.poi.xwpf.usermodel.XWPFTable#getRows()

The following examples show how to use org.apache.poi.xwpf.usermodel.XWPFTable#getRows() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BookMarks.java    From frpMgr with MIT License 6 votes vote down vote up
/** 
 * 构造函数,用以分析文档,解析出所有的标签
 * @param document  Word OOXML document instance. 
 */
public BookMarks(XWPFDocument document) {

	//初始化标签缓存
	this._bookmarks = new HashMap<String, BookMark>();

	// 首先解析文档普通段落中的标签 
	this.procParaList(document.getParagraphs());

	//利用繁琐的方法,从所有的表格中得到得到标签,处理比较原始和简单
	List<XWPFTable> tableList = document.getTables();

	for (XWPFTable table : tableList) {
		//得到表格的列信息
		List<XWPFTableRow> rowList = table.getRows();
		for (XWPFTableRow row : rowList) {
			//得到行中的列信息
			List<XWPFTableCell> cellList = row.getTableCells();
			for (XWPFTableCell cell : cellList) {
				//逐个解析标签信息
				//this.procParaList(cell.getParagraphs(), row);
				this.procParaList(cell);
			}
		}
	}
}
 
Example 2
Source File: RawCopier.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Updates bookmarks for the given output {@link XWPFTable} according to its input {@link XWPFTable}.
 * 
 * @param bookmarkManager
 *            the {@link BookmarkManager}
 * @param outputTable
 *            the output {@link XWPFTable}
 * @param inputTable
 *            the input {@link XWPFTable}
 */
private void updateBookmarks(BookmarkManager bookmarkManager, final XWPFTable outputTable,
        final XWPFTable inputTable) {
    final List<XWPFTableRow> inputRows = inputTable.getRows();
    final List<XWPFTableRow> outputRows = outputTable.getRows();
    for (int rowIndex = 0; rowIndex < inputRows.size(); rowIndex++) {
        final XWPFTableRow inputRow = inputRows.get(rowIndex);
        final XWPFTableRow outputRow = outputRows.get(rowIndex);
        final List<XWPFTableCell> inputCells = inputRow.getTableCells();
        final List<XWPFTableCell> outputCells = outputRow.getTableCells();
        for (int cellIndex = 0; cellIndex < inputCells.size(); cellIndex++) {
            final XWPFTableCell inputCell = inputCells.get(cellIndex);
            final XWPFTableCell outputCell = outputCells.get(cellIndex);
            final List<IBodyElement> inputBodyElements = inputCell.getBodyElements();
            final List<IBodyElement> outputBodyElements = outputCell.getBodyElements();
            for (int bodyElementIndex = 0; bodyElementIndex < inputBodyElements.size(); bodyElementIndex++) {
                final IBodyElement inputBodyElement = inputBodyElements.get(bodyElementIndex);
                if (inputBodyElement instanceof XWPFParagraph) {
                    final IBodyElement outputBodyElement = outputBodyElements.get(bodyElementIndex);
                    updateBookmarks(bookmarkManager, ((XWPFParagraph) outputBodyElement).getCTP(),
                            ((XWPFParagraph) inputBodyElement).getCTP());
                }
            }
        }
    }
}
 
Example 3
Source File: AbstractBodyParser.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parses a {@link Table}.
 * 
 * @param wtable
 *            the table to parse
 * @return the created object
 * @throws DocumentParserException
 *             if a problem occurs while parsing.
 */
protected Table parseTable(XWPFTable wtable) throws DocumentParserException {
    if (wtable == null) {
        throw new IllegalArgumentException("parseTable can't be called on a null argument.");
    }
    Table table = (Table) EcoreUtil.create(TemplatePackage.Literals.TABLE);
    table.setTable(wtable);
    for (XWPFTableRow tablerow : wtable.getRows()) {
        Row row = (Row) EcoreUtil.create(TemplatePackage.Literals.ROW);
        table.getRows().add(row);
        row.setTableRow(tablerow);
        for (XWPFTableCell tableCell : tablerow.getTableCells()) {
            Cell cell = (Cell) EcoreUtil.create(TemplatePackage.Literals.CELL);
            row.getCells().add(cell);
            cell.setTableCell(tableCell);
            AbstractBodyParser parser = getNewParser(tableCell);
            cell.setBody(parser.parseBlock(null, TokenType.EOF));
        }
    }
    return table;
}
 
Example 4
Source File: HeaderFooterBodyContainer.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
@Override
public XWPFTable insertNewTable(XWPFRun run, int row, int col) {
    XmlCursor cursor = ((XWPFParagraph) run.getParent()).getCTP().newCursor();
    XWPFTable table = insertNewTbl(cursor);
    // hack for cursor.removeXmlContents(); in XWPFHeaderFooter
    List<XWPFTableRow> rows = table.getRows();
    for (int i = 0; i < rows.size(); i++) {
        table.removeRow(i);
    }
    for (int i = 0; i < row; i++) {
        XWPFTableRow tabRow = (table.getRow(i) == null) ? table.createRow() : table.getRow(i);
        for (int k = 0; k < col; k++) {
            if (tabRow.getCell(k) == null) {
                tabRow.createCell();
            }
        }
    }
    return table;
}
 
Example 5
Source File: CellBodyContainer.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
@Override
public XWPFTable insertNewTable(XWPFRun run, int row, int col) {
    XmlCursor cursor = ((XWPFParagraph) run.getParent()).getCTP().newCursor();
    XWPFTable table = insertNewTbl(cursor);

    // hack for cursor.removeXmlContents(); in XWPFTableCell
    List<XWPFTableRow> rows = table.getRows();
    for (int i = 0; i < rows.size(); i++) {
        table.removeRow(i);
    }
    for (int i = 0; i < row; i++) {
        XWPFTableRow tabRow = (table.getRow(i) == null) ? table.createRow() : table.getRow(i);
        for (int k = 0; k < col; k++) {
            if (tabRow.getCell(k) == null) {
                tabRow.createCell();
            }
        }
    }
    return table;
}
 
Example 6
Source File: DocPrSupport.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public static void updateDocPrId(XWPFTable table) {
    List<XWPFTableRow> rows = table.getRows();
    rows.forEach(row -> {
        List<XWPFTableCell> cells = row.getTableCells();
        cells.forEach(cell -> {
            cell.getParagraphs().forEach(DocPrSupport::updateDocPrId);
            cell.getTables().forEach(DocPrSupport::updateDocPrId);
        });
    });
}
 
Example 7
Source File: NiceXWPFDocument.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
private void readTables(List<XWPFTable> tables) {
    allTables.addAll(tables);
    for (XWPFTable table : tables) {
        List<XWPFTableRow> rows = table.getRows();
        if (null == rows) continue;;
        for (XWPFTableRow row : rows) {
            List<XWPFTableCell> cells = row.getTableCells();
            if (null == cells) continue;
            for (XWPFTableCell cell : cells) {
                initAllElement(cell);
            }
        }
    }
}
 
Example 8
Source File: TemplateResolver.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
@Override
public List<MetaTemplate> resolveBodyElements(List<IBodyElement> bodyElements) {
    List<MetaTemplate> metaTemplates = new ArrayList<>();
    if (null == bodyElements) return metaTemplates;

    // current iterable templates state
    Deque<BlockTemplate> stack = new LinkedList<BlockTemplate>();

    for (IBodyElement element : bodyElements) {
        if (element == null) continue;
        if (element.getElementType() == BodyElementType.PARAGRAPH) {
            XWPFParagraph paragraph = (XWPFParagraph) element;
            RunningRunParagraph runningRun = new RunningRunParagraph(paragraph, templatePattern);
            List<XWPFRun> refactorRuns = runningRun.refactorRun();
            if (null == refactorRuns) continue;
            Collections.reverse(refactorRuns);
            resolveXWPFRuns(refactorRuns, metaTemplates, stack);
        } else if (element.getElementType() == BodyElementType.TABLE) {
            XWPFTable table = (XWPFTable) element;
            List<XWPFTableRow> rows = table.getRows();
            if (null == rows) continue;
            for (XWPFTableRow row : rows) {
                List<XWPFTableCell> cells = row.getTableCells();
                if (null == cells) continue;
                cells.forEach(cell -> {
                    List<MetaTemplate> visitBodyElements = resolveBodyElements(cell.getBodyElements());
                    if (stack.isEmpty()) {
                        metaTemplates.addAll(visitBodyElements);
                    } else {
                        stack.peek().getTemplates().addAll(visitBodyElements);
                    }
                });
            }
        }
    }

    checkStack(stack);
    return metaTemplates;
}
 
Example 9
Source File: WordDocxTableParser.java    From SnowGraph with Apache License 2.0 4 votes vote down vote up
public static TableInfo parseWordTable(XWPFTable table, String caption) {
    if (table == null) {
        return null;
    }

    TableInfo tableInfo = new TableInfo();

    tableInfo.setTableCaption(caption);
    tableInfo.setRowSize(table.getNumberOfRows());

    List<XWPFTableRow> rows = table.getRows();
    int rowNum = 0;
    // 遍历所有行
    for (XWPFTableRow row : rows) {
        TableRowInfo rowInfo = new TableRowInfo();
        if (rowInfo != null)
            tableInfo.addSubDocumentElement(rowInfo);// 添加到父节点:table

        rowNum++;
        rowInfo.setRowNum(rowNum);

        List<XWPFTableCell> cells = row.getTableCells();
        int colNum = 0;
        // 遍历所有列(单元格)
        for (XWPFTableCell cell : cells) {
            TableCellInfo cellInfo = null;

            int startColNum = colNum + 1;
            int columnSpan = getGridSpan(cell);
            colNum += columnSpan;

            // 判断是否跨行,以及是否是跨行单元格的第一行
            if (isVMerge(cell) && !isVMergeRestart(cell)) {
                // 是跨行单元格的非首行位置,获取到这个合并单元格的对象实例
                TableRowInfo prevRowInfo = rowInfo.getPreviousRow();
                cellInfo = prevRowInfo.getCellAtColumn(startColNum);
                cellInfo.setEndRowNum(rowNum);// 更新该单元格的尾行值
            }
            else {
                // 跨行首格以及其他所有情况。
                cellInfo = new TableCellInfo();
                cellInfo.setStartRowNum(rowNum);
                cellInfo.setEndRowNum(rowNum);
                cellInfo.setStartColNum(startColNum);
                cellInfo.setEndColNum(colNum);
                PlainTextInfo plainTextInfo = new PlainTextInfo(cell.getText().trim());
                //plainTextInfo.setEnglishText();
                cellInfo.addSubDocumentElement(plainTextInfo);
            }

            // 添加到父节点:row
            if (cellInfo != null)
                rowInfo.addSubDocumentElement(cellInfo);
        }

        // table的colsize未初始化时,要根据计算出的colnum,也就是最后一行的编号,来初始化。
        if (tableInfo.getColumnSize() == -1)
            tableInfo.setColumnSize(colNum);
    }

    //第二级数据
    tableInfo.setTableParts(WTTablePartParser.parseTableToParts(tableInfo));
    return tableInfo;
}
 
Example 10
Source File: HackLoopTableRenderPolicy.java    From poi-tl with Apache License 2.0 4 votes vote down vote up
private int getRowIndex(XWPFTable table, XWPFTableRow row) {
    List<XWPFTableRow> rows = table.getRows();
    return rows.indexOf(row);
}