如何获取docx中的表格,单元格占了几行几列

我的docx中有个表格,其中的单元格是合并了其他单元格后组成的大单元格,如何获取该单元格占据了几行几列
image.png (3.8 KB)

docx.docx (10.8 KB)

@humanhuman, 您可以使用 HorizontalMergeVerticalMerge 属性来获取单元格合并信息。 请考虑以下代码示例:

Document doc = new Document("docx.docx");

// Get the first table (index = 0) in the document.
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);

NodeCollection<Row> rows = table.getChildNodes(NodeType.ROW, true);
int row_idx = 1;
for (Row row : rows)
{
    System.out.println("Row #" + row_idx + ":");
    int cell_idx = 1;
    for (Cell cell : row.getCells())
    {
        System.out.println("\tCell #" + cell_idx + ":");

        int verticalMerge = cell.getCellFormat().getVerticalMerge();
        System.out.println("\t\tverticalMerge:" + getMergeInfo(verticalMerge, true));

        int horizontalMerge = cell.getCellFormat().getHorizontalMerge();
        System.out.println("\t\thorizontalMerge: " + getMergeInfo(horizontalMerge, false));

        cell_idx++;
    }
    row_idx++;
}

private static String getMergeInfo(int cellMerge, Boolean isVertical)
{
    if (cellMerge == CellMerge.NONE)
        return "The cell is not merged.";
    if (cellMerge == CellMerge.FIRST && isVertical)
        return "The cell is the first cell in a range of vertically merged cells.";
    if (cellMerge == CellMerge.FIRST && !isVertical)
        return "The cell is the first cell in a range of horizontally merged cells.";
    if (cellMerge == CellMerge.PREVIOUS && isVertical)
        return "The cell is merged to the previous cell vertically.";
    if (cellMerge == CellMerge.PREVIOUS && !isVertical)
        return "The cell is merged to the previous cell horizontally.";

    return "Unknown";
}

以下是您文档的程序输出:

Row #1:
	Cell #1:
		verticalMerge:The cell is not merged.
		horizontalMerge: The cell is not merged.
	Cell #2:
		verticalMerge:The cell is not merged.
		horizontalMerge: The cell is not merged.
	Cell #3:
		verticalMerge:The cell is not merged.
		horizontalMerge: The cell is not merged.
	Cell #4:
		verticalMerge:The cell is not merged.
		horizontalMerge: The cell is not merged.
Row #2:
	Cell #1:
		verticalMerge:The cell is not merged.
		horizontalMerge: The cell is not merged.
	Cell #2:
		verticalMerge:The cell is the first cell in a range of vertically merged cells.
		horizontalMerge: The cell is not merged.
	Cell #3:
		verticalMerge:The cell is not merged.
		horizontalMerge: The cell is not merged.
Row #3:
	Cell #1:
		verticalMerge:The cell is not merged.
		horizontalMerge: The cell is not merged.
	Cell #2:
		verticalMerge:The cell is merged to the previous cell vertically.
		horizontalMerge: The cell is not merged.
	Cell #3:
		verticalMerge:The cell is not merged.
		horizontalMerge: The cell is not merged.
Row #4:
	Cell #1:
		verticalMerge:The cell is not merged.
		horizontalMerge: The cell is not merged.
	Cell #2:
		verticalMerge:The cell is not merged.
		horizontalMerge: The cell is not merged.
	Cell #3:
		verticalMerge:The cell is not merged.
		horizontalMerge: The cell is not merged.
	Cell #4:
		verticalMerge:The cell is not merged.
		horizontalMerge: The cell is not merged.

我知道这个方法,但是无法获取占据了几列,我的这个单元格是原本的四个单元格合并的,应该是两行两列

@humanhuman, 不幸的是,Aspose.Words 没有提供可以做到这一点的方法。 您可能可以使用 HorizontalMerge 和 VerticalMerge 属性自己实现此类方法。 或者,如果您建议此方法原型应该是什么样子,我可以将其记录在票证中并分配给合适的开发人员。