c# - 获取Word文档表的rowspan和cospan

标签 c# vsto openxml openxml-sdk

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

谁能解释一下我如何使用 openXML 获取这两个值?

最佳答案

最后我找到了解决方案,

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

关于c# - 获取Word文档表的rowspan和cospan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30414381/

相关文章:

c# - 如何使用 C# 绑定(bind) .aspx 中定义的现有下拉列表

c# - 两个数据网格具有相同的来源,但输出的结果略有不同

c# - 您能否在 ARM 上的最新 Visual Studio 中构建 VSTO Excel 解决方案?

c# - 如何设置现有的 Explorer.exe 实例来选择文件?

c# - WPF Dispatcher.InvokeAsync() 异步委托(delegate)的奇怪行为

c# - 需要在磁盘上存储 Office Word 2007 加载项 (VSTO) 的设置文件

c# - 使用 VSTO 读取和修改 Word 文档的文本

c# - 如何使用 OpenXML 包自动调整 excel 列

c# - 在 C# 的 excel 文档中执行查找和替换

c# - 将 XLSM 转换为 XLSX