在 C# Word 开发中,可以使用以下代码将现有表格插入到 ContentControl 的 Tables 中:

  1. 获取 ContentControl 对象:
ContentControl cc = document.ContentControls[1]; //假设 ContentControl 对象已经存在
  1. 创建一个新的表格对象:
Table table = document.Tables.Add(cc.Range, rowcount, columncount); //假设现有表格的行数和列数分别为 rowcount 和 columncount
  1. 将现有表格的内容复制到新表格中:
for (int i = 1; i <= rowcount; i++) //逐行复制
{
    for (int j = 1; j <= columncount; j++) //逐列复制
    {
        table.Cell(i, j).Range.Text = existingTable.Cell(i, j).Range.Text; //existingTable 指现有表格对象
    }
}
  1. 将新表格插入到 ContentControl 的 Tables 中:
cc.Range.Tables.Add(table.Range);

以上代码可以将现有表格插入到 ContentControl 的 Tables 中。需要注意的是,如果 ContentControl 的类型不是 'wdContentControlTypeRichText'(即富文本类型),则不支持插入表格。

C# Word 开发:将现有表格插入 ContentControl 的 Tables 中

原文地址: https://www.cveoy.top/t/topic/nuIp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录