使用 C# 在 Word 中访问拆分的 table 单元格
Access splitted table cell in Word with C#
我将一个单元格分成两列:
table.Cell(1,1).Split(1,2);
如何访问这两个新单元格?
与 Word 中的许多内容一样,获取对象 "pointers" 的技巧是使用 Ranges
。
在这种情况下,如果对原始单元格的 Range
进行了实例化,则可以引用它。拆分后它将位于第一个单元格中。从中,可以获得第一个和第二个单元格(实际上,table 中的任何其他内容)。
例如
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";
我将一个单元格分成两列:
table.Cell(1,1).Split(1,2);
如何访问这两个新单元格?
与 Word 中的许多内容一样,获取对象 "pointers" 的技巧是使用 Ranges
。
在这种情况下,如果对原始单元格的 Range
进行了实例化,则可以引用它。拆分后它将位于第一个单元格中。从中,可以获得第一个和第二个单元格(实际上,table 中的任何其他内容)。
例如
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";