0

如何使用 Novacode DocX 从单词表中删除单个单元格?

我努力了:

table.Rows[0].Cells.RemoveAt(2);

Cell c = table.Rows[0].Cells[2];
table.Rows[0].Cells.Remove(c);

table.Rows[0].Cells.RemoveRange(2,4);

他们都没有删除任何单元格。

4

4 回答 4

2

我最近一直在使用 Novacode DocX。我已经意识到,很多类的方法都是继承的,没有被覆盖。一个示例cell继承Remove()container并且永远不会被覆盖以执行您期望它执行的操作。 Novacode.Table 的源代码

您可以尝试解决此问题的是隐藏单元格/单元格,因为我假设您没有删除整行或整列。

Novacode.Table table = document.Tables[0]
Novacode.Row row = table.Rows[0]; //int of row that contains cell/cells to remove
int first_cell = 0; //beginning cell of range
int last_cell = 1; //last cell of range
for(int i = first_cell; i < last_cell + 1; i++)
{
    foreach (var paragraph in row.Cells[i].Paragraphs)
    {
        paragraph.Remove(false);
    }
}
row.MergeCells(first_cell, last_cell);
Novacode.Cell cell = row.Cells[first_cell];
Novacode.Border border = new Border();
border.Tcbs = Novacode.BorderStyle.Tcbs_none;
cell.SetBorder(Novacode.TableCellBorderType.Right, border);
cell.SetBorder(Novacode.TableCellBorderType.Left, border);
cell.SetBorder(Novacode.TableCellBorderType.Top, border);
cell.SetBorder(Novacode.TableCellBorderType.Bottom, border);

此代码将删除文本并合并第一个表格的前两个单元格,并使那里的边框不可见。因此,这会将您要删除的区域变成一个不可见的空白单元格,假设您正在Paragraphs使用Cell.

于 2015-07-10T20:29:09.647 回答
0

您可能必须将表格保存回文档。
尝试

  1. 将表格附加到文档的末尾。如果缺少细胞
  2. 删除并替换文档中的表格。
于 2015-06-21T04:29:02.503 回答
0

您必须自己从单元元素中清除子 XML,因为 NovaCode DocX Api 未正确处理它:

table.Rows.Last().Cells.Last().Xml.RemoveAll();
table.Rows.Last().Cells.Last().InsertParagraph();

请注意,最后的 InsertParagraph 是必要的,因为 Cells 不能为空。

于 2017-02-14T10:35:37.950 回答
0

Table课堂上,您可以使用方法

MergeCells(int, int)

MergeCellsInColumn(int, int, int)

'删除'细胞。

请参阅:https ://docx.codeplex.com/SourceControl/latest#DocX/Table.cs

于 2017-03-29T04:27:15.203 回答