使用 DocX dll 将行添加到 word 文件(doc 文件)中的现有 table
Add rows to existing table in word file (doc file) using DocX dll
我在向现有 table 添加行时遇到问题,
在我的应用程序中,我正在处理 word 文件。
在我的模板(word 文件)中已经有一个 table 有 8 行不包括 header(你可以在图片中看到),在我的应用程序中构建一个新的 word 文件时,有时我想向 table 添加更多行,因为我可能有更多数据要插入(来自 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 将我的 table 填充到 word 文件中,然后我得到一个未处理的 exception-index 超出范围,这是没有意义的,因为如果我不添加行,只是用相同的代码用相同的行号填充 table,我没有例外,table 工作正常,这是我从 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++)
正如我在下面所做的那样,我得到了这个table:如图所示,行中没有单元格和边框,这可能是我不知道的问题。
我找到了这个 link
特别是关于
的部分
this.Tables[1].set_Style("Table Grid 8");
尝试样式部分。
还有这个 link,它显示了一个完整的示例,它使用
table.Borders
这看起来像你要找的东西:)
干杯
荣兰
我在向现有 table 添加行时遇到问题, 在我的应用程序中,我正在处理 word 文件。 在我的模板(word 文件)中已经有一个 table 有 8 行不包括 header(你可以在图片中看到),在我的应用程序中构建一个新的 word 文件时,有时我想向 table 添加更多行,因为我可能有更多数据要插入(来自 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 将我的 table 填充到 word 文件中,然后我得到一个未处理的 exception-index 超出范围,这是没有意义的,因为如果我不添加行,只是用相同的代码用相同的行号填充 table,我没有例外,table 工作正常,这是我从 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++)
正如我在下面所做的那样,我得到了这个table:如图所示,行中没有单元格和边框,这可能是我不知道的问题。
我找到了这个 link
特别是关于
的部分this.Tables[1].set_Style("Table Grid 8");
尝试样式部分。
还有这个 link,它显示了一个完整的示例,它使用
table.Borders
这看起来像你要找的东西:)
干杯
荣兰