I had created a C# function which saves DataGridView
to Excel which works well on most of machines. However, some of machines are throwing following error:
Microsoft.Office.Interop.Excel._Application. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Library not registered. (Exception from HRESULT 0x8002801D (TYPE_E_LIBNOTREGISTERED)).
Is there any way I can fix this issue?
This is my source code:
private bool dataGridView_ExportToExcel(string fileName, DataGridView dgv, string ext, bool chart)
{
CheckForIllegalCrossThreadCalls = false;
Excel.Application excelApp = new Excel.Application();
if (excelApp == null)
{
MessageBox.Show("No Excel has been installed!");
return false;
}
Excel.Workbook wb = excelApp.Workbooks.Add(true);
Excel._Worksheet workSheet = wb.Worksheets.get_Item(1) as Excel._Worksheet;
workSheet.Name = "C#";
// Print Contents
for (int r = 0; r < dgv.Rows.Count; r++)
{
for (int i = 0; i < dgv.Columns.Count; i++)
{
workSheet.Cells[r + 2, i + 1] = dgv.Rows[r].Cells[i].Value;
}
}
workSheet.Columns.AutoFit();
wb.SaveAs(fileName, Excel.XlFileFormat.xlOpenXMLWorkbook, Type.Missing, Type.Missing, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlUserResolution, true, Type.Missing, Type.Missing, Type.Missing);
wb.Close(Type.Missing, Type.Missing, Type.Missing);
excelApp.Quit();
releaseObject(excelApp);
releaseObject(workSheet);
releaseObject(wb);
return true;
}
I am using .NET framework 4.6.1
environment.
Thank you for reading. have a nice day:)
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.