获取Word文档表的rowspan和cospan

本文关键字:rowspan cospan Word 文档 获取 | 更新日期: 2023-09-27 17:50:27

我试图在wordprocessingML中获得表的colspan和rowspan值。

谁能解释我如何能得到这两个值使用openXML?

获取Word文档表的rowspan和cospan

我终于找到了解决办法,

private static int CalcColspan(XElement cell)
            {
                if (cell.Name != W.tc)
                    return 0;
                return cell.Element(W.tcPr).Element(W.gridSpan) == null ? 1 :
                        Convert.ToInt32(cell.Element(W.tcPr).Element(W.gridSpan).Attribute(W.val).Value);
            }
private static int CalcRowspan(XElement cell)
            {
                if (cell.Name != W.tc)
                    return 0;
                int rowspan = 1, colNum = 0;
                XElement currentRow = cell.Parent;
                foreach (XElement tc in cell.NodesBeforeSelf())
                {
                    colNum += CalcColspan(tc);
                }
                bool endOfSpan = false;
                foreach (XElement row in currentRow.NodesAfterSelf())
                {
                    int currentColNum = 0;
                    foreach (XElement tc in row.Nodes())
                    {
                        if (tc.Name != W.tc)
                            continue;
                        if (currentColNum == colNum)
                        {
                            if (tc.Element(W.tcPr).Element(W.vMerge) != null)
                            {
                                if ((string)tc.Element(W.tcPr).Element(W.vMerge).Attribute(W.val) != "restart")
                                {
                                    rowspan++;
                                    break;
                                }
                            }
                            endOfSpan = true;
                        }
                        currentColNum += CalcColspan(tc);
                    }
                    if (endOfSpan)
                        break;
                }
                return rowspan;
            }