Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim excel As New FileInfo(Server.MapPath("Xls/myData.xlsx"))
Using package = New ExcelPackage(excel)
Dim workbook = package.Workbook
'*** Sheet 1
Dim worksheet = workbook.Worksheets.First()
'*** DataTable & DataSource
Dim dt As DataTable = ConvertToDataTable(worksheet)
Me.myGridView.DataSource = dt
Me.myGridView.DataBind()
End Using
End Sub
Private Function ConvertToDataTable(oSheet As ExcelWorksheet) As DataTable
Dim totalRows As Integer = oSheet.Dimension.[End].Row
Dim totalCols As Integer = oSheet.Dimension.[End].Column
Dim dt As New DataTable(oSheet.Name)
Dim dr As DataRow = Nothing
For i As Integer = 1 To totalRows
If i > 1 Then
dr = dt.Rows.Add()
End If
For j As Integer = 1 To totalCols
If i = 1 Then
dt.Columns.Add(oSheet.Cells(i, j).Value.ToString())
Else
dr(j - 1) = oSheet.Cells(i, j).Value.ToString()
End If
Next
Next
Return dt
End Function