将多维列表插入到Excel表单时发生COMexception

我试图找出一种方法来插入一个multi dimensional C# listexcel sheet使用interop assembly我有我的列表中有多行数据分布在8列。 但是,当我执行我的查询,它把我COM Exception from HRESULT: 0x800A03EC ,我有以下代码

 public void ExportStructureListToExcel(List<StructuresDS> listExport, string sheetName) { try { Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); Microsoft.Office.Interop.Excel._Worksheet worksheet1 = null; worksheet1 = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets["Sheet1"]; worksheet1 = (Microsoft.Office.Interop.Excel._Worksheet)workbook.ActiveSheet; for (int i = 1; i < listExport.Count + 1; i++) { for (int j = 1; j < 8; j++) { worksheet1.Cells[i, j] = listExport[i - 1]; } } string fileDestination = @"S:\Parser Project\sde.xls"; if (File.Exists(fileDestination)) { File.Delete(fileDestination); } workbook.SaveAs(fileDestination, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing); workbook.Close(true, Type.Missing, Type.Missing); Process.Start(fileDestination); app.Quit(); } catch (Exception e) { MessageBox.Show(e.Message); } } 

StructureDS结构

  public class StructuresDS { public DateTime time; public string CC; public string term; public string strike; public string strategy; public double? premium; public int volume; public double ratio; public string over; } **Inserting elements to the List** listStructures.Add(new StructuresDS { time = Convert.ToDateTime(AxiomSubSet[0].time.ToString("HH:mm:ss")), CC = AxiomSubSet[0].CC, term = listCodedTerms[0], strike = (Convert.ToDouble(AxiomSubSet[0].strike) * 100).ToString(), strategy = AxiomSubSet[0].strategy, premium = Convert.ToDouble(AxiomSubSet[0].price), volume = Convert.ToInt32(AxiomSubSet[0].quantity) }); 

这个错误是在worksheet1.Cells[i, j] = listExport[i - 1]; 我无法find解决办法。 我可以知道我错在哪里吗?

你正试图设置一个单元格的结构。 您需要将其设置为结构的其中一个字段。 就像是:

 worksheet1.Cells[i, j].Value = listExport[i - 1].over; 

那( .over )可能是错误的领域,因为我不知道你真的想把哪一个放入单元格。