1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
private object Missing = System.Reflection.Missing.Value;
private Excel.Application App_exc_lecture;
private Excel.Workbook Classeur_exc_lecture;
private Excel.Sheets AllFeuilles_exc_lecture;
private Excel.Worksheet Feuille_ItemOPC_lecture;
private Excel.Range Range_exc_lecture;
try
{
App_exc_lecture = new Excel.Application();
}
catch (Exception)
{
MessageBox.Show("ATTENTION ! Application Excel introuvable");
return;
}
// Ouverture classeur
try
{
App_exc_lecture.Workbooks.Close();
Classeur_exc_lecture =
App_exc_lecture.Workbooks.Open(NomFichierExcelRep, Missing,Missing,Missing, Missing,Missing, Missing, Missing, Missing,Missing, Missing, Missing, Missing,Missing, Missing);
AllFeuilles_exc_lecture = Classeur_exc_lecture.Sheets;
Feuille_ItemOPC_lecture = (Excel.Worksheet)AllFeuilles_exc_lecture["Etors"]; // Feuille
Range_exc_lecture = Feuille_ItemOPC_lecture.get_Range("A2","H5000"); //Lignes 2 à fin
MessageBox.Show(Feuille_ItemOPC_lecture.Cells[3,4].ToString());
}
catch (Exception)
{
MessageBox.Show("PROBLEME : Classeur " + NomFichierExcelRep + " introuvable ");
return;
}
finally // a faire toujours
{
// Fermeture classeur
System.Runtime.InteropServices.Marshal.ReleaseComObject(Classeur_exc_lecture);
System.Runtime.InteropServices.Marshal.ReleaseComObject(App_exc_lecture);
GC.Collect(); // Force le garbage collector à nettoyer les process
} |
Partager