VB.net: How to get data from Excel worksheet to string object?
-
11-07-2019 - |
Question
I've tried this:
'start Excel app
Dim exApp As Microsoft.Office.Interop.Excel.Application
exApp = CreateObject("Excel.Application")
' load excel document
exApp.Workbooks.Open(fname)
Dim exSheet As Microsoft.Office.Interop.Excel.Worksheet
exSheet = exApp.Workbooks(1).Worksheets(1)
and than, for example accessing "C3" cell:
Dim b As String
b = exSheet.Cells("A3")
or:
b = exSheet.Cells(3,3)
and it throws me an exception. I'm feeling that I'm doing something wrong with the object access, but this method worked in embedded VB, and do not works in .net. Also, tried to google the exception code, with no relevant result.
Solution
Try:
b = exSheet.Range("A3").Value.ToString
OTHER TIPS
I don't think you should write code in VB6 style for vb.net.
Looking at the code example, I think what you need is
b = exSheet.Cells(3,3).Text
or
b = exSheet.Cells(3,3).Value
EDIT: I guess it the reference should be assigned to an instance of range.
So, the code might look like
Range exampleRange = exSheet.Cells(3,3)
b = exampleRange.Text 'OR it can be b = exampleRange.Value
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow