0
I am trying to generate an excel file from a datagrid, and this is generating the following error:
An unhandled Exception of type 'System.Invalidcastexception' occurred in Eletronictaxnotes.exe
Additional information: It is not possible to convert the 'Microsoft.Office.Interop.Excel.Applicationclass' COM object to the 'Microsoft.Office.Interop. _Application'. This operation failed because the Queryinterface call in the COM component for the IID interface '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Error loading library/DLL of type. (Exception of HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)).
private void ExportarExcel2()
{
// Creating a Excel object.
Excel._Application excel = new Excel.Application();
if (excel == null)
{
MessageBox.Show("Excel is not properly installed!!"); return;
}
Excel._Workbook workbook = excel.Workbooks.Add(Type.Missing);
try
{
Excel._Worksheet worksheet = workbook.ActiveSheet;
worksheet.Name = "ExportedFromDatGrid";
worksheet.Cells[1, 0] = "Codigo";
worksheet.Cells[1, 1] = "Descrição";
worksheet.Cells[1, 2] = "Fornecedor";
worksheet.Cells[1, 3] = "Quantidade";
worksheet.Cells[1, 4] = "Preço";
worksheet.Cells[1, 5] = "Preço Total";
worksheet.Cells[1, 6] = "Ncm";
worksheet.Cells[1, 7] = "Nota";
worksheet.Cells[1, 8] = "DataDocumento";
worksheet.Cells[1, 9] = "Tipo";
// Passa as celulas do DataGridView para a Pasta do Excel
for (var i = 0; i <= gridItens.RowCount - 1; i++)
{
for (var j = 0; j <= 9; j++)
{
DataGridViewCell cell = j <= 2 ? gridItens[j, i] : gridNotas[j, i];
worksheet.Cells[i + 2, j + 1] = cell.Value;
}
}
//Getting the location and file name of the excel to save from user.
var saveDialog = new SaveFileDialog
{
Filter = @"Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*",
FilterIndex = 2
};
if (saveDialog.ShowDialog() != DialogResult.OK) return;
workbook.SaveAs(saveDialog.FileName);
MessageBox.Show(@"Export Successful");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
excel.Quit();
}
}
I followed the following suggestions to include the appropriate references:
What I need to use Microsoft.Office.Interop.Excel in . NET?
Cannot find Microsoft.Office.Interop Visual Studio
I have also tried other suggestions, which consists of deleting a duplicate record that could be causing the error:
Error Printing When Using Microssoft.office.Interop
But the record that is giving error does not exist in my records, as picture below:
Similarly the following suggestions did not help me either:
Error accessing COM Components
Class not Registered error when Creating Excel Workbook in C#
Unfortunately none of this worked, the result being always the same:
I am running out of options to proceed. I imagined that such a task would be something simple to do. Does anyone have any other solution suggestions that can help me?
Post the error texts and paste the codes here, avoid using images. Also note the use of tags. It is not necessary to use the tag
visual-studio
on questions that are not about the IDE.– Jéf Bueno
Thank you, I made the corrections you suggested.
– Pedro Mapelli