0

I have one simple problem when trying to write List into the excel workbook. on string its work perfect but problem is how i can put list into excel

public List<string> _RoomType = new List<string>();
Excel.Range RoomType = (Excel.Range)_sheet.get_Range(_sheet.Cells[22, "B"] as Excel.Range, _sheet.Cells[25, "B"] as Excel.Range);
 for (int i = 0; i < _RoomType.Count; i++)
            {
                RoomType.set_Value(Type.Missing, _RoomType[i]);

if im using for loop it sets from 22B to 25B only first value which is in list and if i dont use 'for' visual studio gives me exception : Exception from HRESULT: 0x800A03EC can anyone help me?

Irakli Lekishvili
  • 33,492
  • 33
  • 111
  • 169
  • Inspire yourself from here: http://stackoverflow.com/questions/1059951/export-a-c-list-of-lists-to-excel Does this help ? – Trefex Jul 28 '11 at 10:44

1 Answers1

2

You need to pass a 2-dimensional array to the set_Value method. You have to make sure though that number of items in your list equals the number of cells in your range.

Object[,] dataArray = new object[1, _RoomType.Count];
for (int i = 0; i < _RoomType.Count; i++)
{
   dataArray[0, i] = _RoomType[i];
}
RoomType.set_Value(Type.Missing, dataArray);
Edwin de Koning
  • 14,209
  • 7
  • 56
  • 74