Share via


How to put array into excel range

Question

Sunday, August 28, 2011 10:26 PM

I know how to write single cell into excel but when im trying it on array excel sheet is filling with only last value

this is my range

 Excel.Range ServiceName = (Excel.Range)_sheet.get_Range(_sheet.Cells[38, "B"] as Excel.Range, _sheet.Cells[45, "B"] as Excel.Range);

_ServiceName is List which contains 1,2,3,4,5,6

for (int i = 0; i < _ServiceName.Count; i++)
      {
        ServiceNameArray[0, i] = _ServiceName[i];

      }

this i my trying to write into excel but as i said it there is only last item (6) in excel book

for (int i = 0; i < _ServiceName.Count; i++)
      {
        ServiceName.set_Value(Type.Missing, ServiceNameArray[0,i]);
      }

does anyone have an idea?

All replies (3)

Monday, August 29, 2011 2:17 PM âś…Answered

Hello,

Consider this code sample:

Excel.Range range = worksheet.get_Range("A1", "B5");

object[,] values = new object[5, 2]; // row, column on worksheet
values[0, 0] = 11; values[0, 1] = 12;
values[1, 0] = 21; values[1, 1] = 22;
values[2, 0] = 31; values[2, 1] = 32;
values[3, 0] = 41; values[3, 1] = 42;
values[4, 0] = 51; values[4, 1] = 52;

range.set_Value(Excel.XlRangeValueDataType.xlRangeValueDefault, values);
Marshal.ReleaseComObject(range);

 

Regards from Belarus (GMT + 2),

Andrei Smolin
Add-in Express Team Leader


Monday, August 29, 2011 12:04 PM

Excel.Range ServiceName = (Excel.Range)_sheet.get_Range("B38", "B45");

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

I haven't work with Excel interop in a while but, should't yoube indexing ServiceName to call set_Value?

Something like:

 

Paulo Morgado


Monday, August 29, 2011 12:37 PM

      object[,] obj = (object[,])this.Range["a1", "a5"].Value2;

      foreach (var str in obj)
      {
        MessageBox.Show(str.ToString());

      }

I hope this helpshttp://vsto.tistory.com