我怎样才能导入我的列表框到Excel与C#

我想在Excel文件中导入我的ListBox。 但我不断得到这个错误HRESULT:0x800A03EC可以有人帮我我google了,但没有find任何东西

我的代码

string ExcelFileLocation = (@"C:\Users\bra\Desktop\EXCELFILe"); Excel.Application oApp; Excel.Worksheet oSheet; Excel.Workbook oBook; oApp = new Excel.Application(); oBook = oApp.Workbooks.Add(); oSheet = (Excel.Worksheet) oBook.Worksheets.get_Item(1); int i = 0; i++; for (int j = 0; j < listBox1.Items.Count; j++) { oSheet.Cells[j, 1] = listBox1.Items; } oBook.SaveAs(ExcelFileLocation); oBook.Close(); oApp.Quit(); 

我的ListBox项目

  private void Form1_Load(object sender, EventArgs e) { listBox1.Items.Add("TEST1"); listBox1.Items.Add("TEST2"); listBox1.Items.Add("TEST3"); } 

你的variables“j”必须从1开始而不是0。

您的代码试图将所有项目 (即ListBox.Items的集合分配给Excel中的每个单元格。

你可能打算做的是类似的

 for (int j = 0; j < listBox1.Items.Count; j++) { oSheet.Cells[j + 1, 1] = listBox1.Items[j].ToString(); // Notice the [j] indexer } 

更重要的是,excel索引从1开始,像@ mdelpeix正确注意到的。 这很可能是抛出你的问题是关于特定的例外,所以他的答案应该可能得到答案标记:)