使用 C# 向 word 文档添加行 [英] Add row to word doc using C#
问题描述
我使用 c# 在 word 文档中创建了一个表格,我试图在表格的末尾添加行.现在我有 4 行的表.
I created a table in word document using c#, I am trying to add row to the end of the table. for now I have table with 4 rows.
我试着这样添加:
int rowIndex=1;
this.Tables[1].Rows.Add(this.Tables[1].Rows[rowIndex]);
来源.如果我设置 int rowIndex=1;
它将行添加到表的开头,当我尝试将其设置为 int rowIndex=4;
它将行添加到行之前.
Source. if I set int rowIndex=1;
it adds row to the begining of the table, when I tried to set it to int rowIndex=4;
it adds row to the row before last.
我需要表的最后一行,所以我尝试了int rowIndex=5;
,但后来我得到:
I need a new last row at the end of the table, so I tried int rowIndex=5;
, but then I get :
未处理的异常:System.Runtime.InteropServices.COMException:请求的集合成员不存在.在 Microsoft.Office.Interop.Word.Rows.get_Item(Int32 Index)
Unhandled Exception: System.Runtime.InteropServices.COMException: The requested member of the collection does not exist. at Microsoft.Office.Interop.Word.Rows.get_Item(Int32 Index)
推荐答案
尝试:
object oMissing = System.Reflection.Missing.Value;
this.Tables[1].Rows.Add(ref oMissing);
这篇关于使用 C# 向 word 文档添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!