简体   繁体   中英

C#. microsoft interop excel

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM