I am having problem with the code I wrote for exporting datagridview to excel on button_click event. The error
cannot create an instance of the abstract class or interface 'Microsoft.Office.Interop.Excel._Application'
shows at this line of code
new Microsoft.Office.Interop.Excel._Application();
the object library in the reference is microsoft excel 14.0 object libary . I am using VS Ultimate 2013. The Ms Office is 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();
I expect to successully export datagrivew to excel.
You should use the class Excel.Application
.
You are trying to instantiate the interface Excel._Application
which is not possible.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.