How to kill excel.exe for that particular opened excel in vb.net (window application)
-
Hi, I try to export datagrid data to one excel spread sheet (i called it ExcelA). Then, when I want to close ExcelA, I wrote this codes to kill excel.exe. wExcel.Quit() System.Runtime.InteropServices.Marshal.ReleaseComObject(wExcel) System.GC.Collect() Dim proc As System.Diagnostics.Process For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL") proc.Kill() Next wExcel = Nothing But then, it killed all the excel that I currently open. (those that are not related to this ExcelA.) How can i only kill the excel.exe that belonging to this ExcelA ? Please help. I had tried to search.. but failed to get it. Thanks in advanced. Appreciate your help. :) Here is my codes: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ds.Clear() ds.Tables.Add() If ds.Tables(0).Columns.Count <= 0 Then For i As Integer = 0 To myTable.Columns.Count - 1 ds.Tables(0).Columns.Add(myTable.Columns(i).ColumnName) Next End If Dim dr1 As DataRow For k As Integer = 0 To myTable.Rows.Count - 1 dr1 = ds.Tables(0).NewRow For j As Integer = 0 To myTable.Columns.Count - 1 dr1(j) = dgSearch.Item(k, j) Next ds.Tables(0).Rows.Add(dr1) Next Dim wExcel As New Excel.ApplicationClass Dim wBook As Excel.Workbook Dim wSheet As Excel.Worksheet wBook = wExcel.Workbooks.Add() wSheet = wBook.ActiveSheet() Dim dt As System.Data.DataTable = ds.Tables(0) Dim dc As System.Data.DataColumn Dim dr As System.Data.DataRow Dim colIndex As Integer = 0 Dim rowIndex As Integer = 0 For Each dc In dt.Columns colIndex = colIndex + 1 wExcel.Cells(1, colIndex) = dc.ColumnName Next For Each dr In dt.Rows rowIndex = rowIndex + 1 colIndex = 0 For Each dc In dt.Columns colIndex = colIndex + 1 wExcel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName) Next Next wSheet.Columns.AutoFit() Dim strFileName As String = "C:\M2.xls" Dim blnFileOpen As Boolean = False Try Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName) fileTemp.Close() Catch ex As Exception
-
Hi, I try to export datagrid data to one excel spread sheet (i called it ExcelA). Then, when I want to close ExcelA, I wrote this codes to kill excel.exe. wExcel.Quit() System.Runtime.InteropServices.Marshal.ReleaseComObject(wExcel) System.GC.Collect() Dim proc As System.Diagnostics.Process For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL") proc.Kill() Next wExcel = Nothing But then, it killed all the excel that I currently open. (those that are not related to this ExcelA.) How can i only kill the excel.exe that belonging to this ExcelA ? Please help. I had tried to search.. but failed to get it. Thanks in advanced. Appreciate your help. :) Here is my codes: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ds.Clear() ds.Tables.Add() If ds.Tables(0).Columns.Count <= 0 Then For i As Integer = 0 To myTable.Columns.Count - 1 ds.Tables(0).Columns.Add(myTable.Columns(i).ColumnName) Next End If Dim dr1 As DataRow For k As Integer = 0 To myTable.Rows.Count - 1 dr1 = ds.Tables(0).NewRow For j As Integer = 0 To myTable.Columns.Count - 1 dr1(j) = dgSearch.Item(k, j) Next ds.Tables(0).Rows.Add(dr1) Next Dim wExcel As New Excel.ApplicationClass Dim wBook As Excel.Workbook Dim wSheet As Excel.Worksheet wBook = wExcel.Workbooks.Add() wSheet = wBook.ActiveSheet() Dim dt As System.Data.DataTable = ds.Tables(0) Dim dc As System.Data.DataColumn Dim dr As System.Data.DataRow Dim colIndex As Integer = 0 Dim rowIndex As Integer = 0 For Each dc In dt.Columns colIndex = colIndex + 1 wExcel.Cells(1, colIndex) = dc.ColumnName Next For Each dr In dt.Rows rowIndex = rowIndex + 1 colIndex = 0 For Each dc In dt.Columns colIndex = colIndex + 1 wExcel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName) Next Next wSheet.Columns.AutoFit() Dim strFileName As String = "C:\M2.xls" Dim blnFileOpen As Boolean = False Try Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName) fileTemp.Close() Catch ex As Exception