XLDotNet : Quitter Excel sans laisser d'instance en RAM v1.0.*Table des procédures 1 - AssemblyInfo.vb 2 - clsExcelHost.vb 2.1 - Public Sub New 2.2 - Public Sub Quitter 3 - Form1.vb 3.1 - <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent 3.2 - Private Sub Button1_Click 3.3 - Private Sub Button2_Click 3.4 - Protected Overloads Overrides Sub Dispose 3.5 - Public Sub New AssemblyInfo.vb Imports System Imports System.Reflection Imports System.Runtime.InteropServices ' Les informations générales relatives à un assembly dépendent de l'ensemble ' d'attributs suivant. Pour modifier les informations associées à un assembly, 'changez les valeurs de ces attributs. ' Vérifiez les valeurs des attributs de l'assembly <Assembly: AssemblyTitle("XLDotNet : Quitter Excel sans laisser d'instance en RAM")> <Assembly: AssemblyDescription("XLDotNet : Quitter Excel sans laisser d'instance en RAM")> <Assembly: AssemblyCompany("ORS Production")> <Assembly: AssemblyProduct("XLDotNet")> <Assembly: AssemblyCopyright("ORS Production")> <Assembly: AssemblyTrademark("XLDotNet")> <Assembly: AssemblyCulture("")> 'Le GUID suivant est pour l'ID de la typelib si ce projet est exposé à COM <Assembly: Guid("F5077ACD-CF54-45AD-8518-C68F44019577")> ' Les informations de version pour un assembly se composent des quatre valeurs suivantes : ' ' Version principale ' Version secondaire ' Numéro de build ' Révision ' ' Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut ' en utilisant '*', comme indiqué ci-dessous : <Assembly: AssemblyVersion("1.0.*")> clsExcelHost.vb ' ====================================================================================== ' clsExcelHost : Classe pour héberger Excel ' ============ ' Title: EXCEL.EXE Process Killer ' Description: After many weeks of trying to figure out why the EXCEL.EXE Process ' does not want to go away from the Task Manager, I wrote this class that will ensure ' that the correct EXCEL.EXE Process is closed. This is after using Excel.Application ' via Automation from a VB.NET/ASP.NET application. ' This file came from Planet-Source-Code.com... the home millions of lines of source code ' You can view comments on this code/and or vote on it at: ' http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1998&lngWId=10 ' The author may have retained certain copyrights to this code... ' please observe their request and the law by reviewing all copyright conditions ' at the above URL. ' Author: I.W Coetzer 2004/01/22 ' *Thanks Dan for the process idea. ' Classe commentée et légèrement modifiée par Patrice Dargenton le 05/11/2004 ' *Solution to the EXCEL.EXE Process that does not want to go away from task manager. ' ' IMPLEMENTATION (EXAMPLE OF THE CLASS IN USE) ' 'Public Sub TestXL() ' Dim oXLH As clsExcelHost ' Try ' oXLH = New clsExcelHost ' Catch ' MsgBox("Excel n'est pas installé !") ' Exit Sub ' End Try ' oXLH.xlApp.Workbooks.Add() ' oXLH.xlApp.Range("A1") = "Hello World!" ' oXLH.xlApp.Workbooks(1).SaveAs("C:\Test.xls") ' oXLH.xlApp.Workbooks(1).Close() ' oXLH.xlApp.Quit() ' oXLH.xlApp = Nothing ' oXLH.Quitter() ' = Process.GetProcessById(xl.ProcId).Kill() ' oXLH = Nothing ' MsgBox("C:\Test.xls a été créé avec succès !") ' Dim p As New Process ' p.StartInfo = New ProcessStartInfo("C:\Test.xls") ' p.Start() 'End Sub ' ' ====================================================================================== Public Class clsExcelHost Public xlApp As Object Private ProcId% Public Sub New() ProcId = 0 ' Liste des processus avant le mien Dim Process1() As Process = Process.GetProcesses() xlApp = CreateObject("Excel.Application") ' Liste des processus après le mien : la différence me donnera l'Id du mien Dim Process2() As Process = Process.GetProcesses() Dim i%, j% Dim bMonProcessXL As Boolean For j = 0 To Process2.GetUpperBound(0) If Process2(j).ProcessName = "EXCEL" Then bMonProcessXL = True ' Parcours des processus avant le mien For i = 0 To Process1.GetUpperBound(0) If Process1(i).ProcessName = "EXCEL" Then If Process2(j).Id = Process1(i).Id Then ' S'il existait avant, ce n'était pas le mien bMonProcessXL = False Exit For End If End If Next i If bMonProcessXL = True Then ' Maintenant que j'ai son Id, je pourrai le tuer ' xlApp.Hinstance ne fonctionne pas avec Excel 2000 ' alors que cette méthode marche toujours ! ProcId = Process2(j).Id Exit For End If End If Next j End Sub Public Sub Quitter() If ProcId = 0 Then Exit Sub Process.GetProcessById(ProcId).Kill() End Sub 'Protected Overrides Sub Finalize() ' MyBase.Finalize() 'End Sub End Class Form1.vb Public Class Form1 Inherits System.Windows.Forms.Form #Region " Code généré par le Concepteur Windows Form " Public Sub New() MyBase.New() 'Cet appel est requis par le Concepteur Windows Form. InitializeComponent() 'Ajoutez une initialisation quelconque après l'appel InitializeComponent() End Sub 'La méthode substituée Dispose du formulaire pour nettoyer la liste des composants. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Requis par le Concepteur Windows Form Private components As System.ComponentModel.IContainer 'REMARQUE : la procédure suivante est requise par le Concepteur Windows Form 'Elle peut être modifiée en utilisant le Concepteur Windows Form. 'Ne la modifiez pas en utilisant l'éditeur de code. Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar Friend WithEvents Button2 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button Me.StatusBar1 = New System.Windows.Forms.StatusBar Me.Button2 = New System.Windows.Forms.Button Me.SuspendLayout() ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(16, 16) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(480, 32) Me.Button1.TabIndex = 0 Me.Button1.Text = "Test XL sans laisser d'instance en RAM" ' 'StatusBar1 ' Me.StatusBar1.Location = New System.Drawing.Point(0, 117) Me.StatusBar1.Name = "StatusBar1" Me.StatusBar1.Size = New System.Drawing.Size(512, 24) Me.StatusBar1.TabIndex = 1 ' 'Button2 ' Me.Button2.Location = New System.Drawing.Point(16, 64) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(480, 32) Me.Button2.TabIndex = 2 Me.Button2.Text = "Test XL : méthode classique : laisse une nouvelle instance d'Excel en RAM à chaqu" & _ "e appel" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(512, 141) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.StatusBar1) Me.Controls.Add(Me.Button1) Me.Name = "Form1" Me.Text = "XLDotNet : Quitter Excel sans laisser d'instance en RAM" Me.ResumeLayout(False) End Sub #End Region Private Sub Button1_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim oXLH As clsExcelHost Me.StatusBar1.Text = "Ouverture d'Excel..." Me.Cursor = Cursors.WaitCursor Try oXLH = New clsExcelHost Catch Me.StatusBar1.Text = "" MsgBox("Excel n'est pas installé !") Exit Sub End Try Me.StatusBar1.Text = "Ecriture du fichier Excel..." oXLH.xlApp.Workbooks.Add() oXLH.xlApp.Range("A1") = "Bonjour !" Dim sChemin$ = Application.StartupPath & "\Test.xls" ' Désactiver les messages d'alerte, par exemple si le fichier existe déjà oXLH.xlApp.DisplayAlerts = False Me.StatusBar1.Text = "Sauvegarde du fichier " & sChemin & "..." oXLH.xlApp.Workbooks(1).SaveAs(sChemin) Me.StatusBar1.Text = "Fermeture d'Excel..." oXLH.xlApp.Workbooks(1).Close() oXLH.xlApp.Quit() oXLH.xlApp = Nothing oXLH.Quitter() ' = Process.GetProcessById(xl.ProcId).Kill() oXLH = Nothing Me.StatusBar1.Text = "" Me.Cursor = Cursors.Default MsgBox(sChemin & " a été créé avec succès !") Me.StatusBar1.Text = "Ouverture du fichier Excel..." Me.Cursor = Cursors.WaitCursor Dim p As New Process p.StartInfo = New ProcessStartInfo(sChemin) p.Start() Me.Cursor = Cursors.Default Me.StatusBar1.Text = "" End Sub Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click Dim oXL As Object Me.StatusBar1.Text = "Ouverture d'Excel..." Me.Cursor = Cursors.WaitCursor Try oXL = CreateObject("Excel.Application") Catch Me.StatusBar1.Text = "" MsgBox("Excel n'est pas installé !") Exit Sub End Try Me.StatusBar1.Text = "Ecriture du fichier Excel..." oXL.Workbooks.Add() oXL.Range("A1") = "Bonjour !" Dim sChemin$ = Application.StartupPath & "\Test.xls" ' Désactiver les messages d'alerte, par exemple si le fichier existe déjà oXL.DisplayAlerts = False Me.StatusBar1.Text = "Sauvegarde du fichier " & sChemin & "..." oXL.Workbooks(1).SaveAs(sChemin) Me.StatusBar1.Text = "Fermeture d'Excel..." oXL.Workbooks(1).Close() oXL.Quit() oXL = Nothing Me.StatusBar1.Text = "" Me.Cursor = Cursors.Default MsgBox(sChemin & " a été créé avec succès !") Me.StatusBar1.Text = "Ouverture du fichier Excel..." Me.Cursor = Cursors.WaitCursor Dim p As New Process p.StartInfo = New ProcessStartInfo(sChemin) p.Start() Me.Cursor = Cursors.Default Me.StatusBar1.Text = "" End Sub End Class