I am using the following code to write the data to Excel Sheet, but the Excel process is still ON (in Task Manager) because of it Excel Sheet opens as ReadOnly, which doesn't allow me to write the another data successfully as the Sheet opened as ReadOnly mode.
Have done more analysis by accessing the .Net Forum as well, But no success.
Can you please guide me where the disconnect is?
Attaching the code here....
Code: Select all
Public Shared Sub ExcelWrite (ByVal ExcelPath As String, ByVal SheetName As String, ByVal RowNo As Integer, ByVal ColumnName As String, ByVal WriteValue 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)
xcRange.Value = WriteValue
xcWB.Save()
Catch e As Exception
Report.Error("Writing the Data to Excel Sheet - Unexpected Exception Occured"+ e.ToString())
End Try
'clean up
xcRange = Nothing
xcWS = Nothing
xcWB.Close()
xcWB = Nothing
xcApp.Quit()
xcApp = Nothing
TerminateExcel()
End Sub
Private Shared Sub TerminateExcel()
Dim ClassName As String = "XLMain"
Dim WindowHandle As Int32
Dim ReturnVal As Int32
Const WM_QUIT = &H12
Do
WindowHandle = FindWindow(ClassName, Nothing)
If WindowHandle Then
ReturnVal = PostMessage(WindowHandle, WM_QUIT, 0, 0)
End If
Loop Until WindowHandle = 0
End Sub
Dal...