How to Insert an Excel Spreadsheet into a VB Form

It is useful to know how to view a Microsoft Excel spreadsheet in a Visual Basic .NET project for further data manipulation. Microsoft Excel is a spreadsheet application included in the Microsoft Office suite. It provides many useful tools for analyzing large amounts of data. Visual Basic is a programming language developed by Microsoft and is preferred by many programmers for its flexibility. In just a few steps, you can create a Visual Basic project to display content from an Excel spreadsheet.

Step 1

Open Microsoft Excel and type “A” in “A1,” “B” in “B1”, “Column A” in “A2”, and “Column B” in “B2.” Save the spreadsheet in “C: \\” as “ExcelFile.xlsx.”

Step 2

Open Microsoft Visual Basic 2010 Express, click the “File” menu and select “New Project”. Click “Installed Templates”, select “Windows Forms Application” and click “OK”

Step 3

Press “Ctrl” + “Alt” + “X” to open the “Toolbox” window. Double-click “DataGridView” to add a new Data Grid View control to “Form1”. Double-click “Button” in “Toolbox” to add a new button to “Form1”.

Step 4

Double click on “Button1” to open the form “Form1.vb”. Type the following “Public Class Form1”: Import System.Data.OleDb

Step 5

Type the following in “Private Sub Button1_Click” to declare a “DataSet” and define the Excel connection: Dim ds As new DataSet () Dim connectionString As String = “Provider = Microsoft.ACE.OLEDB.12.0;” & _ “Data Source = C: \\ ExcelFile.xlsx;” & _ “Extended Properties = Excel 12.0;”

Step 6

Type the following to connect to the “ExceFile.xlsx” file and fill in the “DataSet”: Dim excelData as new OleDbDataAdapter (“SELECT * FROM [Sheet1 $]”, connectionString) excelData.TableMappings.Add (“Table”, “ExcelSheet” ) excelData.Fill (ds)

Type the following to view the spreadsheet in data grid view: Me.DataGridView1.DataSource = ds.Tables (0) Me.Refresh () Press “F5” to run the program and press “Button1” to import the spreadsheet Excel.