gpt4 book ai didi

c# - 使用 Microsoft.Office.Interop 向 MS Word 表格添加一行

转载 作者:太空狗 更新时间:2023-10-29 18:31:47 25 4
gpt4 key购买 nike

我有一个带有表格的单词模板,我从使用制表符拆分的字符串列表中填充该表格。

我不知道我会有多少行文本,因为它会有所不同。

所以我在像这样遍历我的循环之前以编程方式添加一行:

 oWordDoc.Tables[2].Rows.Add(oWordDoc.Tables[2].Rows[1]);

不幸的是,它是在当前行之前而不是之后添加行。

如何更改我的代码以始终在当前行之后添加一个空行?

最佳答案

将参数值保留为 Row.Add 函数的缺失值

object oMissing = System.Reflection.Missing.Value;        
// get your table or create a new one like this
// you can start with two rows.
Microsoft.Office.Interop.Word.Table myTable = oWordDoc.Add(myRange, 2,numberOfColumns)
int rowCount = 2;
//add a row for each item in a collection.
foreach( string s in collectionOfStrings)
{
myTable.Rows.Add(ref oMissing);
// do something to the row here. add strings etc.
myTable.Rows[rowCount].Cells[1].Range.Text = "Content of column 1";
myTable.Rows[rowCount].Cells[2].Range.Text = "Content of column 2";
myTable.Rows[rowCount].Cells[3].Range.Text = "Content of column 3";
//etc
rowCount++;
}

我没有测试过这段代码,但应该可以工作......

关于c# - 使用 Microsoft.Office.Interop 向 MS Word 表格添加一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20662818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com