C#。微软互操作excel。

C#。微软互操作excel。,c#,excel,interop,C#,Excel,Interop,我正在尝试将RoomType数组插入excel手册。RoomType的范围是从D22到D25,所以问题是此代码仅将firts值置于此范围内。如果我将RoomType.set_值插入for循环,则excel范围将填充最后一个数组项。有人能帮我吗 Object[,] RoomtypeArray = new object[1, _RoomType.Count]; for (int i = 0; i < _RoomType.Count; i++) {

我正在尝试将RoomType数组插入excel手册。RoomType的范围是从D22到D25,所以问题是此代码仅将firts值置于此范围内。如果我将RoomType.set_值插入for循环,则excel范围将填充最后一个数组项。有人能帮我吗

 Object[,] RoomtypeArray = new object[1, _RoomType.Count];
     for (int i = 0; i < _RoomType.Count; i++)
                {
                    RoomtypeArray[0, i] = _RoomType[i];

                }
 RoomType.set_Value(Type.Missing, RoomtypeArray);
Object[,]RoomtypeArray=新对象[1,_RoomType.Count];
对于(int i=0;i<\u RoomType.Count;i++)
{
RoomtypeArray[0,i]=\u RoomType[i];
}
RoomType.set_值(Type.Missing,RoomtypeArray);
这就是您需要的:

//Microsoft.Office.Interop.Excel.Range RoomType;
//List<double> _RoomType;

object[,] roomTypeArray = Array.CreateInstance(
    typeof(object),
    new int[] { 1, _RoomType.Count},
    new int[] { 1, 1 });

for (int i = 0; i < _RoomType.Count; i++)
{
    roomTypeArray[1, i + 1] = _RoomType[i];
}

RoomType.Value2 = roomTypeArray;
//Microsoft.Office.Interop.Excel.Range RoomType;
//列表-房间类型;
对象[,]roomTypeArray=Array.CreateInstance(
类型(对象),
新的int[]{1,_RoomType.Count},
新的int[]{1,1});
对于(int i=0;i<\u RoomType.Count;i++)
{
roomTypeArray[1,i+1]=\u RoomType[i];
}
RoomType.Value2=roomTypeArray;
因为为范围设置数组需要基于1的索引,而不是基于0的索引,这些索引与
C
中的
new
station一起使用


(也可以在的公认答案中找到一个介于object[,]和double[,]之间的巧妙技巧,以便在Excel InReop中使用)。

什么是“房间类型”?RoomType与之相同。RoomType是范围,而_RoomType是列表