[RESOLVED] - Cleaning up memory after launching Excel from button
Here's my little problem:
Currently I am using a button on a windows form to transfer the contents of a datagrid into an Excel Spreadsheet and view the resulting sheet in Excel:
Each time the button is pressed during the same session (for different queries etc) a new process instance of "EXCEL.EXE" is added to the task manager, eating up about 15 Mb of memory (or more), even after the user has closed the Excel app. These processes go away only when the windows program is closed. Is there a way I can have my application purge these orphaned processes within the same session after the Excel app has been closed so that I don't eat up so much memory for no reason?
I have attached my button code for transfering the data and launching Excel below:
Here's my little problem:
Currently I am using a button on a windows form to transfer the contents of a datagrid into an Excel Spreadsheet and view the resulting sheet in Excel:
Each time the button is pressed during the same session (for different queries etc) a new process instance of "EXCEL.EXE" is added to the task manager, eating up about 15 Mb of memory (or more), even after the user has closed the Excel app. These processes go away only when the windows program is closed. Is there a way I can have my application purge these orphaned processes within the same session after the Excel app has been closed so that I don't eat up so much memory for no reason?
I have attached my button code for transfering the data and launching Excel below:
VB.NET:
Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
' display wait cursor
Me.Cursor = Cursors.WaitCursor
' various variables
Dim row As Integer
Dim col As Integer
Dim rowCount As Integer
Dim colCount As Integer
Dim cell As String
Dim rowcell As Integer
' Excel Variables
Dim excelApp As New Excel.Application
Dim excelBook As Excel.Workbook = excelApp.Workbooks.Add
Dim excelWorksheet As Excel.Worksheet = _
CType(excelBook.Worksheets(1), Excel.Worksheet)
' get count of rows and count of columns
rowCount = objDataSet.Tables(0).Rows.Count()
colCount = objDataSet.Tables(0).Columns.Count()
' add the column headings
For col = 0 To colCount - 1
row = 1
cell = GetExcelColumn(col) & row.ToString
excelWorksheet.Range(cell).Value = grdFieldnetData.TableStyles(0).GridColumnStyles(col).HeaderText
excelWorksheet.Range(cell).ColumnWidth = grdFieldnetData.TableStyles(0).GridColumnStyles(col).Width / 4
Next
' now add the data elements
For row = 0 To rowCount - 1
rowcell = row + 2
For col = 0 To colCount - 1
cell = GetExcelColumn(col) & rowcell.ToString
excelWorksheet.Range(cell).Value = grdFieldnetData.Item(row, col).ToString()
Next
Next
' turn off wait cursor
Me.Cursor = Cursors.Default
' view the spread sheet
excelApp.Visible = True
End Sub
Last edited: