I have this code in a button to import excel (which I got online as well)
Imports System.Data
Imports Microsoft.Office.Interop
Public Class Template
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim xlRange As Excel.Range
Dim sFileName As String
Dim xlRow As Integer
With OpenFileDialog1
.Title = "Import Excel"
.FileName = ""
.Filter = "Excel File|*.xlsx;*.xls"
If .ShowDialog() = DialogResult.OK Then
sFileName = .FileName
If Trim(sFileName) <> "" Then
xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Open(sFileName)
xlWorkSheet = xlWorkBook.Worksheets("Template")
xlRange = xlWorkSheet.UsedRange
Dim rowcount As Integer = 17
For xlRow = 17 To xlRange.Rows.Count '//data starts at R17C1
If xlRange.Cells(xlRow, 1).Text <> String.Empty Then
rowcount = 1
DataGridView1.Rows.Add(rowcount, xlRange.Cells(xlRow, 1).Text,
xlRange.Cells(xlRow, 2).Text,
xlRange.Cells(xlRow, 3).Text,
xlRange.Cells(xlRow, 4).Text,
xlRange.Cells(xlRow, 5).Text,
xlRange.Cells(xlRow, 6).Text,
xlRange.Cells(xlRow, 7).Text,
xlRange.Cells(xlRow, 8).Text,
xlRange.Cells(xlRow, 9).Text,
xlRange.Cells(xlRow, 10).Text)
End If
Next
End If
End If
End With
End Class
Now I am having an error System.MissingMemberException
:
I am stuck for days and no solution has been found online. I tried using .ToString instead of .Text but it only I got System_.Comobject displayed in datagrid. I have the correct path and sheet name. Please help me solve this error. I am pretty new with .Net so guide and treat me as a beginner.
CodePudding user response:
I have found a solution for my question. I used the code below and it worked.
DataGridView1.Rows.Add(xlRange.Range("A" & xlRow.ToString).Text, ...)