Regarding release of excel.exe
hi,
This is my code and I don't know why it's not releasing the excel object.Excel.exe is working on the background.please reply me as soon as possible.
Thanks
Public Function MakeExcelFile() As Boolean
Dim bReturn As Boolean = True
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
'xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add
xlWorkSheet = xlWorkBook.Worksheets.Add 'Sheets("Sheet1")
Try
Dim dt As Date = System.DateTime.Today()
xlWorkSheet.Cells(1, 5) = "Project Detail"
With xlWorkSheet.Cells(1, 5)
With .Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = True
.ColorIndex = 5
End With
End With
xlWorkSheet.Cells(2, 5) = "Created on: " & dt.ToLongDateString
Dim nStartRow As Integer = 16
'Try
' Dim xlRange As Excel.Range = xlWorkSheet.UsedRange
' xlRange.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Activate()
' nStartRow = xlApp.ActiveCell.Row + 2
'Catch ex As Exception
' nStartRow = 16
'End Try
'Document parameters:
With xlWorkSheet.Rows(nStartRow)
With .Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = False
.ColorIndex = 1
End With
End With
xlWorkSheet.Cells(nStartRow, 2) = "Item ID"
xlWorkSheet.Cells(nStartRow, 2).Interior.ColorIndex = 15
xlWorkSheet.Cells(nStartRow, 3) = "NTP Detail ID"
xlWorkSheet.Cells(nStartRow, 3).Interior.ColorIndex = 15
xlWorkSheet.Cells(nStartRow, 4) = "Item Name"
xlWorkSheet.Cells(nStartRow, 4).Interior.ColorIndex = 15
xlWorkSheet.Cells(nStartRow, 5) = "Unit"
xlWorkSheet.Cells(nStartRow, 5).Interior.ColorIndex = 15
xlWorkSheet.Cells(nStartRow, 6) = "Submit Date"
xlWorkSheet.Cells(nStartRow, 6).Interior.ColorIndex = 15
''Drawing Comments
nStartRow += 2
If Not m_dtReportTable Is Nothing Then
For Each dtRow As DataRow In m_dtReportTable.Rows
xlWorkSheet.Cells(nStartRow, 2) = dtRow("TPItemID")
xlWorkSheet.Cells(nStartRow, 3) = dtRow("NTPDetailID")
xlWorkSheet.Cells(nStartRow, 4) = dtRow("ItemName")
xlWorkSheet.Cells(nStartRow, 5) = dtRow("Unit")
xlWorkSheet.Cells(nStartRow, 6) = Convert.ToDateTime(dtRow("InputDateTime")).Date
nStartRow += 1
Next
End If
'MakeTempFileName(TransmittalFileName, ".XLS")
xlWorkSheet.SaveAs(m_strFileName)
Catch ex As Exception
bReturn = False
Finally
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlWorkSheet)
releaseObject(xlWorkBook)
releaseObject(xlApp)
End Try
'Dim proc As System.Diagnostics.Process
'For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL")
' proc.Kill()
'Next
Return bReturn
End Function
Private Sub releaseObject(ByVal obj As Object)
Try
While 1
If System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) < 0 Then
Exit While
End If
End While
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub