When I run the following code with no break points set I am getting an: "Object reference not set to an instance of an object." exception from the package.workbook being nothing as soon as I try to interrogate the object ("If Not IsNothing(package.Workbook) AndAlso Not IsNothing(package.Workbook.Worksheets) AndAlso package.Workbook.Worksheets.Count > 0") to ensure that the workbook has been read and there is something there to work with.
When I run under debug and give it a few seconds to complete whatever operation it is doing in the background (i.e. would appear to be generating the workbook object) all works well. What I mean by this is that as soon as the code breaks at this point;
If Not IsNothing(package.Workbook) AndAlso Not IsNothing(package.Workbook.Worksheets) AndAlso package.Workbook.Worksheets.Count > 0 Then
When I hover over the package.workbook object it reports that it is nothing ... after a few seconds it starts to report that it is as expected (47 worksheets). This is with NO steps being taken in the debugging. So it would appear to my small mind like their is an action happing in the background that I need to get the code to wait to complete. I have tried a thread,sleep and this does not seem to help.
Any ideas?
The following is the code (which I again need to point out works fine when you allow it to work slowly under debug).
Private Function ReadExcelFileUsingEPP(ByRef strExcelFileName As String) As ExcelSheet()
ReadExcelFileUsingEPP = Nothing
Try
Dim arrTemp() As ExcelSheet = Nothing
Dim blnWorkbookRead As Boolean = False
Try
Dim myMemoryStream As Stream = OpenPackageAsMemoryStream(strExcelFileName)
Dim package As New ExcelPackage(myMemoryStream)
If Not IsNothing(package.Workbook) AndAlso Not IsNothing(package.Workbook.Worksheets) AndAlso package.Workbook.Worksheets.Count > 0 Then
ReDim arrTemp(package.Workbook.Worksheets.Count - 1)
Dim intWorkSheetsNotNothing As Integer = -1
For s As Integer = 1 To package.Workbook.Worksheets.Count
If Not IsNothing(package.Workbook.Worksheets(s)) AndAlso Not IsNothing(package.Workbook.Worksheets(s).Dimension) AndAlso Not IsNothing(package.Workbook.Worksheets(s).Dimension.End) AndAlso Not IsNothing(package.Workbook.Worksheets(s).Dimension.End.Row) AndAlso package.Workbook.Worksheets(s).Dimension.End.Row > 0 AndAlso Not IsNothing(package.Workbook.Worksheets(s).Dimension.End.Column) AndAlso package.Workbook.Worksheets(s).Dimension.End.Column > 0 Then
Console.WriteLine("Reading Sheet: " & package.Workbook.Worksheets(s).Name)
Dim myExcelSheet As ExcelSheet = ReadExcelWorkSheetUsingEPP(package.Workbook.Worksheets(s))
If Not IsNothing(myExcelSheet.SheetName) Then
intWorkSheetsNotNothing += 1
arrTemp(intWorkSheetsNotNothing) = myExcelSheet
End If
End If
Next
ReDim Preserve arrTemp(intWorkSheetsNotNothing)
End If
ReadExcelFileUsingEPP = arrTemp
package.Dispose()
blnWorkbookRead = True
Catch ex As Exception
Console.WriteLine("clsReportGenerator.ReadExcelFileUsingEPP Internal Error: " & ex.Message)
LogError("clsReportGenerator.ReadExcelFileUsingEPP Internal Error: ", ex.Message)
End Try
Catch ex As Exception
Console.WriteLine("clsReportGenerator.ReadExcelFileUsingEPP Error: " & ex.Message)
LogError("clsReportGenerator.ReadExcelFileUsingEPP", ex.Message)
Dim inner As Exception = ex.InnerException
While Not IsNothing(inner)
LogError("clsReportGenerator.ReadExcelFileUsingEPP.inner", inner.Message)
inner = inner.InnerException
End While
End Try
GC.Collect()
GC.WaitForPendingFinalizers()
End Function