【问题标题】:Add rows to existing table in word file (doc file) using DocX dll使用 DocX dll 将行添加到 word 文件(doc 文件)中的现有表
【发布时间】:2016-10-11 08:30:44
【问题描述】:

我在向现有表中添加行时遇到问题, 在我的应用程序中,我正在处理 word 文件。 在我的模板(word 文件)中已经有一个包含 8 行的表,不包括标题(你可以在图片中看到),当在我的应用程序中构建一个新的 word 文件时,有时我想在表中添加更多行,因为也许我有更多数据要插入(来自 dataGridView 的数据)。就像在下面的代码中一样,我根据我的 datagridview 中的行数添加行,问这个:

using (DocX document = DocX.Load(filename))
{
    int k = 0;

    Table t = document.Tables[0];

    // Specify some properties for this Table.
    t.Alignment = Alignment.right;

    if (howManyRows > t.RowCount)
    {
        int x = howManyRows - t.RowCount;
        for (int i = 0; i < x; i++)
        {
            Row row = t.InsertRow();             
        }
    }      

我也试过了:t.InsertRow();

然后我在我的 dataGridView 的 word 文件中填充我的表,然后我得到一个未处理的异常索引超出范围,这是没有意义的,因为如果我不添加行,只是用相同的行填充表编号相同的代码,我没有例外,表工作正常,这是我从 dataGridView 的字符串列表中填充的代码:

for (int i = 1; i <=howManyRows; i++)
{
    for (int j = 4; j >= 0; j--)
    {
        t.Rows[i].Cells[j].Paragraphs.First().Append(dataFromDataGrid[k]).FontSize(11).Font(new FontFamily("Arial"));
        k++;
    }
}
document.Save();

我认为我不需要添加行,因为如果我在我的循环中运行直到:

 for (int i = 1; i <howManyRows; i++)

不喜欢:

for (int i = 1; i <=howManyRows; i++)

正如我在下面所做的那样,我得到了这张表:如图所示,行中没有单元格和边框,也许这是我不知道的问题。

My Word Table

【问题讨论】:

    标签: c# ms-word docx doc


    【解决方案1】:

    我发现了这个link

    特别是关于

    的部分

    this.Tables[1].set_Style("Table Grid 8");

    尝试样式部分。

    另外,这个link,它显示了一个完整的例子,它使用了

    table.Borders
    

    看起来像你要找的东西:)

    干杯

    伊然

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-09
      • 1970-01-01
      • 2016-11-20
      • 2021-10-20
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 1970-01-01
      相关资源
      最近更新 更多