C#。 微软interop excel

当我试图将List写入Excel工作簿时,有一个简单的问题。 在string的工作完美,但问题是我怎么可以把列表放入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]); 

如果即时通讯使用for循环它设置从22B到25B只有在列表中的第一个值,如果我不使用'为'视觉工作室给我例外:HRESULT:0x800A03ECexception任何人都可以帮助我吗?

您需要将2维数组传递给set_Value方法。 您必须确保列表中的项目数量等于您范围内的单元格数量。

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