Bonjour à tous,
j'ai un gros soucis, et je sollicite votre aide en cela. J'ai un DataGridView et je souhaiterais en transférer les données dans un fichier Excel pour impression: par votre aide, j'ai fait ceci.
[using System.Reflection;
using ExcelApplication = Microsoft.Office.Interop.Excel.Application;
]
[ #region DECLARATION
private string UnFichier = "Bulletin.xls";
private string NomEntete = "Notation";
#endregion
]
[#region ExporterGridVersExcel Surchargé Type #1
///<summary>
///Permet d'exporter un DataGrid vers excel
///</summary>
/// <param name="dgView">Data Grid Source des données à Exporter vers Excel</param>
///<param name="unFichier">Fichier Excel de destination des données</param>
///<param name="strEnteteDeFichier">Libellé de l'en-tête du fichier à générer</param>
public void ExporterGridVersExcel(DataGridView dgView, String unFichier, string strEnteteDeFichier)
{
int i = 0;
int j = 0;
ExcelApplication excel = new ExcelApplication();
Microsoft.Office.Interop.Excel.Workbook exbook = (Microsoft.Office.Interop.Excel.Workbook)excel.Workbooks.Add(Missing.Value);
Microsoft.Office.Interop.Excel.Worksheet exsheet = (Microsoft.Office.Interop.Excel.Worksheet)excel.ActiveSheet;
try
{
//Mise en forme de l'en-tête de la feuille Excel
exsheet.Cells[1, 1] = strEnteteDeFichier;
Microsoft.Office.Interop.Excel.Range r = exsheet.get_Range(Convert.ToChar(65 + i).ToString() + "1", Missing.Value);
r.Interior.ColorIndex = Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic;
r.Font.Bold = true;
r.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
r.EntireColumn.AutoFit();//Fin de la mise en forme de l'en-tête.
foreach (DataGridViewColumn ch in dgView.Columns)
{
if (exsheet.Cells[j, i] != null)
{
r = exsheet.get_Range(Convert.ToChar(65 + i).ToString() + "1", Missing.Value);
exsheet.Cells[2, i + 1] = ch.Name.Trim();
r.Interior.ColorIndex = Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic;
r.Font.Bold = true;
r.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
r.EntireColumn.AutoFit();
i++;
}
}
j = 3;
foreach (DataGridViewRow uneLigne in dgView.Rows)
{
i = 1;
foreach (DataGridViewColumn uneColonne in dgView.Columns)
{
if (exsheet.Cells[j, i] != null)
{
r = exsheet.get_Range(Convert.ToChar(65 + i - 1).ToString() + j.ToString(), Missing.Value);
exsheet.Cells[j, i] = "'" + uneLigne.Cells[uneColonne.Name].Value.ToString().Trim();
r.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, Missing.Value);
r.EntireColumn.AutoFit();
i++;
}
}
exsheet.Columns.AutoFit();
j++;
}
exsheet.SaveAs(unFichier, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
excel.Quit();
}
catch (Exception ex)
{
throw (ex);
}
finally
{
excel.Quit();
}
}
#endregion]
[ private void btnExcel_Click(object sender, EventArgs e)
{
ExporterGridVersExcel(dgvMoyenne,UnFichier,NomEntete);
}
]
Mais j'obtiens l'erreur suivante:
[Impossible d'effectuer un cast d'un objet COM de type 'Microsoft.Office.Interop.Excel.ApplicationClass' en type d'interface 'Microsoft.Office.Interop.Excel._Application'. Cette opération a échoué, car l'appel QueryInterface sur le composant COM pour l'interface avec l'IID '{000208D5-0000-0000-C000-000000000046}' a échoué en raison de l'erreur suivante : n’a pas pu être trouvé. (Exception de HRESULT : 0x80030002 (STG_E_FILENOTFOUND)).][/quote]
Quelqu'un peut-il me dire d'où me provient cette erreur?
Merci beaucoup à tous.