I already know the basic procedure of how to read specific cells of an .xls file using VB.NET but I can't figure out how to automatically get all the data from such a file.
Basically, I'm obtaining the data like this:
Dim xlApp As Excel.Application
Dim wb As Workbook
Dim ws As Worksheet
xlApp = New Excel.Application
wb = xlApp.Workbooks.Open("myfile.xls")
ws = wb.Worksheets("worksheet1")
For Each cell In ws.Range("A1", "C10")
Console.WriteLine(cell.value)
Next
In this case, I know that there will be content in the first 10 rows of the columns A, B and C but what would I have to do if I had a huge document whose size and content might even change from time to time? I know that there is also an attribute ws.rows.count but this always returns a big value like 60000 even if only a few rows are occupied.
So basically, I'm looking for a simple way to loop through all the used rows of an Excel file with the possibility of accessing each cell in that row.
in VBA - as distinct from VB.NEt (so I may be off target here) you would either use
(the first line as needed to "reset" the UsedRange,
SpecialCells(xlCellTypeLastCell)
will often give a larger range than is actually present), or more commonlyor
Dim aDataTemp = xl.Worksheets(sSheetName).UsedRange.value
Still in VBA, we often use the
End
statement for this kind of issue:Change the column name or number to fit your needs (or find it in the same way as the last row with
End(xlLeft)
Here's an example that I have used in the past.
It is easy to use Spire.XLS to read all available cells, for example: