Write data and Search a certain data from excel using Visual Basic

  • Thread starter Thread starter fiz_kekemato
  • Start date Start date
F

fiz_kekemato

Guest
Hello... I am new to Visual Basic .Net. I need help.

I have already found a way to read the data from excel to DataGridView in the form.

But I do not know how to write data and search a certain data from the whole data.

How to write data? And how to search a certain data from the whole data in the form?

This is the code for the read part. How can i continue to do the write and search?

Imports System.Data.OleDb
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop
Public Class Form1
Dim SheetList As New ArrayList
Retrieve all Worksheet names and store them into an ArrayList object
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objExcel As Excel.Application
Dim objWorkbook As Excel.Workbook
Dim objWorksheets As Excel.Worksheet
Dim ExcelSheetName As String = ""
objExcel = CreateObject("Excel.Application")
objWorkbook = objExcel.Workbooks.Open("C:\Users\Student\Desktop\NXP\data\data.csv")
For Each objWorksheets In objWorkbook.Worksheets
SheetList.Add(objWorksheets.Name)
ListBox1.Items.Add(objWorksheets.Name)
Next
End Sub
Retrieve specific Worksheet content and display on DataGridview
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim DS As DataSet
Dim MyCommand As OleDb.OleDbDataAdapter
Dim MyConnection As OleDb.OleDbConnection
MyConnection = New OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; " & "data source=C:\Users\Student\Desktop\New folder\data.xlsx; " & "Extended Properties=Excel 8.0;")
Select the data from Sheet1 of the workbook
MyCommand = New OleDb.OleDbDataAdapter("select * from [" & SheetList(0) & "$]", MyConnection)
DS = New System.Data.DataSet()
MyCommand.Fill(DS)
DataGridView1.DataSource = DS.Tables(0).DefaultView
MyConnection.Close()
End Sub
End Class

Could somebody please help me?

Thank you...

Continue reading...
 
Back
Top