获取Word文档表的rowspan和cospan

时间:2015-05-23 15:22:20

标签: c# vsto openxml openxml-sdk

我正在尝试在wordprocessingML中获取表的colspan和rowspan值。

任何人都可以解释我如何使用openXML获取这两个值吗?

1 个答案:

答案 0 :(得分:3)

最后我找到了解决方案,

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;
            }