如何使用Microsoft.Office.Interop.Exceldynamic增加单元格

我正在尝试开发一个可重复使用的List来优化方法。 使用此示例,“ https://msdn.microsoft.com/en-us/library/dd264733.aspx ”

我看到单元格被单独引用,

workSheet.Cells[1, "A"] = "ID Number"; 

我希望能够自动填充列名称,并且从左到右dynamic地显示每个单元格。 我可以想象有一个更好的办法,比直接input“A”,“B”,“A1”等工作方式,或者甚至比存储字母表更好的方法。

这是我到目前为止,

 public static void DownloadExcelFile<T>(List<T> list, string fileName) { Excel.Application excelApp = new Excel.Application(); // Make the object visible. excelApp.Visible = true; excelApp.Workbooks.Add(); Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet; //create column headings foreach (System.ComponentModel.PropertyDescriptor descriptor in System.ComponentModel.TypeDescriptor.GetProperties(list)) { //how can I reference workSheet.Cells by row, and have the //column incremented somehow within the loop? workSheet.Cells[1, "A"] = descriptor.name; } } 

您可以使用序号而不是字母代码:

 int colIndex = 1; foreach (System.ComponentModel.PropertyDescriptor descriptor in System.ComponentModel.TypeDescriptor.GetProperties(list)) { workSheet.Cells[1, colIndex++] = descriptor.Name; }