Microsoft.Office.Interop.Excel不使用索引添加新行

我试图将列表转换为Excel文件。 但我不认为这是通过使用WorkSheet.Cells[i, y] = "value" 。 我想知道是否有一个通用的方法来添加行在Excel中的最后一行的末尾。

 public void AttorneysToExcelFile(List<Scraper.StaggingAttorney> liveScraperAttorneys, string FileName) { Excel.Workbook WorkBook = ExcelApp.Workbooks.Add(); Excel.Worksheet xlWorkSheet = (Excel.Worksheet)WorkBook.Worksheets.get_Item(1); xlWorkSheet.Name = "No Category"; liveScraperAttorneys = liveScraperAttorneys.OrderBy(x=>x.Department).ToList(); xlWorkSheet.Cells[1, 1] = "caseNumber"; xlWorkSheet.Cells[1, 2] = "Category"; xlWorkSheet.Cells[1, 3] = "Names"; xlWorkSheet.Cells[1, 4] = "Email"; xlWorkSheet.Cells[1, 5] = "Address"; for (int i=2; i<liveScraperAttorneys.Count; i++) { Scraper.StaggingAttorney attorney = liveScraperAttorneys[i]; xlWorkSheet.Cells[i, 1] = attorney.caseNumber; xlWorkSheet.Cells[i, 2] = attorney.Category; xlWorkSheet.Cells[i, 3] = attorney.Names; xlWorkSheet.Cells[i, 4] = attorney.Email; xlWorkSheet.Cells[i, 5] = attorney.Address; } WorkBook.SaveAs(FileName); WorkBook.Close(); } 

正如你所看到的,我依靠我。 如果我能立即做到这一点也会有所帮助。 另外我想知道是否添加标题的方式是正确的做法。

迭代互操作对象是非常缓慢的。 将列表转换为object[,] ,然后将其分配给Excel range.Value 。 将标题行添加为数组中的第一个元素,然后将数据作为其余元素添加。 range是与您的数组大小相同的单元格范围。 也就是说,列表中的项目数量是由对象中属性的数量决定的。