【问题标题】:Access splitted table cell in Word with C#使用 C# 在 Word 中访问拆分的表格单元格
【发布时间】:2019-06-26 08:43:37
【问题描述】:

我将一个单元格分成两列:

table.Cell(1,1).Split(1,2);

如何访问这两个新单元格?

【问题讨论】:

  • 如果能提供更多代码以便我们可以复制/粘贴作为测试的起点,这将有很大帮助。然后我们也会知道table 是如何派生的,例如。换句话说,minimal reproducible example - 它可以节省那些从工作日中抽出时间和精力的人,而不需要从头开始猜测和重新创建......

标签: c# ms-word interop


【解决方案1】:

与 Word 中的许多内容一样,获取对象“指针”的诀窍是使用 Ranges

在这种情况下,如果一个指向原始单元格的Range 被实例化,则可以重新引用它。拆分后,它将位于第一个单元格中。从中可以获得第一个和第二个单元格(实际上,还有表格中的其他任何内容)。

例如

Word.Table tbl = document.Tables[1];
Word.Cell cel = tbl.Cell(1, 1);
Word.Range rng = cel.Range;
cel.Split(1, 2);
//At this point, rng is at the start of the first (left-most) cell of the two
//using new objects for the split cells
Word.Cell newCel1 = rng.Cells[1];
Word.Cell newCel2 = rng.Next(wdCell, 1).Cells[1];
newCel1.Range.Text = "1";
newCel2.Range.Text = "2";
//Alternative: using the original cell, plus a new object for the split cell
//Word.Cell newCel2 = rng.Next(Word.WdUnits.wdCell, 1).Cells[1];
//cel.Range.Text = "1";
//newCel2.Range.Text = "2";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多