I am tring to export datagridview content to excel but it throws exception. I have a method
private void exportDataGridToExcel(DataGridView grd)
{
if (saveFileToExcel.ShowDialog() != DialogResult.Cancel)
{
Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
Workbook wb;
Worksheet ws;
wb = Excel.Workbooks.Add();
ws = (Worksheet)wb.Worksheets.get_Item(1);
for (int i = 0; i < grd.Columns.Count + 1; i++)
{
ws.Cells[1, i] = grd.Columns[i - 1].HeaderText;
}
for (int i = 0; i <= grd.Rows.Count; i++)
{
for (int j = 0; j <= grd.Columns.Count; j++)
{
ws.Cells[i + 1, j + 1] = grd.Rows[i - 1].Cells[j].Value.ToString();
}
}
wb.SaveAs(saveFileToExcel.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal);
wb.Close(ws);
Excel.Quit();
}
}
and when i click on button it calls that method like this
private void btnCheck_Click(object sender, EventArgs e)
{
exportDataGridToExcel(dataDaily);
}
but it throws exception in this line of code Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
this exception:
"An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll
Additional information: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))."