开发者

C#. microsoft interop excel.

Im trying to insert RoomType array into the excel book. Range of RoomType is From D22 To D25 so problem is that this code only put the firts value In this range. if i insert RoomType.set_Value into the for loop, excel range filling with last array item. can anyone help me?

 Object[,] RoomtypeArray = new object[1, _RoomType.Count];
     for开发者_如何学JAVA (int i = 0; i < _RoomType.Count; i++)
                {
                    RoomtypeArray[0, i] = _RoomType[i];

                }
 RoomType.set_Value(Type.Missing, RoomtypeArray);


This is what you need:

//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;

because setting an array for a range requires 1-based indexes instead of 0-based which are used with the new statment in C#.

(look also in the accepted answer of How can I quickly up-cast object[,] into double[,]? to find a neat trick going between object[,] and double[,] for use in Excel Inerop).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜