【发布时间】:2019-08-09 16:04:15
【问题描述】:
我为在 button_click 事件上将 datagridview 导出到 excel 而编写的代码有问题。 错误
无法创建抽象类或接口“Microsoft.Office.Interop.Excel._Application”的实例
显示在这行代码
new Microsoft.Office.Interop.Excel._Application();
参考中的对象库是 microsoft excel 14.0 对象库。我正在使用 VS Ultimate 2013。Ms Office 是 2010
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 Worksheet = null;
Worksheet = workbook.Sheets["Sheet1"];
Worksheet = workbook.ActiveSheet;
Worksheet.Name = "StudentDetail";
for (int i=1; i < dataGridView1.Columns.Count+1; i++ )
{
Worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
for (int i=0; i < dataGridView1.Rows.Count; i++ )
{
for (int j=1; j <dataGridView1.Columns.Count; j++ )
{
Worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
var saveFileDialogue = new SaveFileDialog();
saveFileDialogue.FileName = "Output";
saveFileDialogue.DefaultExt = ".xlsx";
if(saveFileDialogue.ShowDialog()==DialogResult.OK)
{
workbook.SaveAs(saveFileDialogue.FileName, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing);
}
app.Quit();
我希望能成功地将 datagrivew 导出到 excel。
【问题讨论】: