I am using the following code to read value from an Excel Sheet. The probelm is whenever this function is getting called the new EXCEL.EXE is getting added in Task Manager. Its not killing the earlier Excel Processes.
Can you please suggest me what would be the change I need to make in my code?
Code: Select all
Public Shared Function ExcelRead (ByVal ExcelPath As String, ByVal SheetName As String, ByVal RowNo As Integer, ByVal ColumnName As String, ByRef CellValue As String) As String
Dim xcApp As New Excel.Application
Dim xcWB As Excel.Workbook = xcApp.Workbooks.Open(ExcelPath)
Dim xcWS As Excel.Worksheet = CType(xcWB.Worksheets(SheetName), Excel.Worksheet)
Dim xcRange As Excel.Range
Dim columnCount As Integer
Try
xcRange = xcWS.UsedRange
For columnCount = 1 To xcRange.Columns.Count
Dim cellData As String
xcRange = xcWS.Cells(1, columnCount)
cellData = xcRange.Text
If cellData = ColumnName Then
Exit For
End If
Next
xcRange = xcWS.Cells(RowNo,columnCount)
CellValue = xcRange.Text
Marshal.ReleaseComObject(xcRange)
Catch e As Exception
Return Nothing
Report.Error("Reading the Data from Excel Sheet - Unexpected Exception Occured"+ e.ToString())
End Try
Return CellValue
'clean up
xcRange = Nothing
xcWS = Nothing
xcWB.Close()
xcWB = Nothing
xcApp.Quit()
xcApp = Nothing
End Function
Dal...