动态填充 excel 文件

Dynamically fill an excel file

我正在尝试动态创建 excel 文件。这是我的代码,但我不知道如何将特定字符串写入每个单元格。我只想在所有单元格中使用相同的字符串。

示例(我正在尝试遵循的示例)在每个单元格中写入相同的文本,但我想更改

using Microsoft.Office.Interop.Excel;

Range aRange = ws.get_Range("A1", "A"+words.Length);
//Obtain the range
if (aRange == null){ 
    Console.WriteLine("Could not get a range. Check to be sure you have the correct versions of the office DLLs.");
}

Object[] args = new Object[1];
//   args[0] = 6; <- this is the default

for (var i = 0; i < words.Length; i++) {
   args[0] = words[i];
}
aRange.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, aRange, args);

我已经添加了 for 循环,但我不知道如何将其写入每个单元格、每个单词。

编辑:在所有行中,系统写入单词[last]

您的对象数组的大小需要与字数相同。

Object[] args = new Object[words.Length];

然后您可以在 for 循环中将单词正确分配给该数组的项目。

args[i] = words[i];

在您的代码中,您在循环的每次迭代中覆盖数组 (args[0]) 中 项的值,以最后一个词结束作为价值。