使用C#访问Word中的拆分表单元格

时间:2019-06-26 08:43:37

标签: c# ms-word interop

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

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

如何访问两个新单元格?

1 个答案:

答案 0 :(得分: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";