Example, we want to read data from sheet1, column A is first name, column B is last name, column C is age. The Excel file (change the name to your file name) in this example is in the same folder as the executable, if diffrerent you would alter the DataSource property in the Builder variable.
VS2010 code, Option Strict On, Option Infer On
Requires 1 ComboBox, 2 Labels
Code:Imports System.Data.OleDb Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim Builder As New OleDbConnectionStringBuilder With { .Provider = "Microsoft.ACE.OLEDB.12.0", .DataSource = IO.Path.Combine(Application.StartupPath, "File1.xlsx") } ' Note HRD=No indicates the first row of the sheet is not data Builder.Add("Extended Properties", "Excel 12.0; HDR=No;") Dim SheetName As String = "Sheet1" Dim dt As New DataTable Using cn As New OleDbConnection With { .ConnectionString = Builder.ConnectionString } cn.Open() Dim cmd As OleDbCommand = New OleDbCommand( <Text> SELECT F1 As FirstName, F2 As LastName, F3 As Age FROM [<%= SheetName %>$] </Text>.Value, cn ) dt.Load(cmd.ExecuteReader) dt.DefaultView.Sort = "LastName asc" End Using ComboBox1.DisplayMember = "LastName" ComboBox1.DataSource = dt Label1.DataBindings.Add("Text", dt, "FirstName") Label2.DataBindings.Add("Text", dt, "Age") End Sub End Class




Reply With Quote
