I have an excel spreadsheet with 22 columns, and undefined amount of rows. I need vb.net to read the data from excel. Then use that data to populate an existing access database table that has been added to visual studio. I've tried multiple approaches and have come to a standstill.
this was approach 1
Expand|Select|Wrap|Line Numbers
- Imports System.Data.OleDb
- Imports System.Data.SqlClient
- Imports Excel = Microsoft.Office.Interop.Excel
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImportFile.Click
- 'Open File Dialog to Find the file.
- FD.InitialDirectory = "C:\"
- FD.Title = "Choose an Excel File to load"
- FD.Filter = "Excel files (*.xls)|*.xls|All files (*.*)|*.*"
- FD.ShowDialog()
- 'Open File
- FD.OpenFile()
- 'Read Excel Filei
- Dim xlApp As Excel.Application
- Dim xlWB As Excel.Workbook
- Dim xlSheet As Excel.Worksheet
- Dim range As Excel.Range
- Dim intRowCount As Integer
- Dim intColumnCount As Integer
- Dim taMeasurement As New FOSRLab3DataSetTableAdapters.Sample_MeasurementsTableAdapter
- xlApp = New Excel.ApplicationClass
- xlWB = xlApp.Workbooks.Open(FD.FileName)
- xlSheet = xlWB.Worksheets(1)
- range = xlSheet.UsedRange
- Dim ta As New FOSRLab3DataSetTableAdapters.Sample_MeasurementsTableAdapter
- For intRowCount = 2 To range.Rows.Count
- SiteID = CType(range.Cells(intRowCount, 1), Excel.Range)
- drSiteID.SiteName = SiteID.ToString
- Next
- xlWB.Close()
- xlApp.Quit()
- releaseObject(xlApp)
- releaseObject(xlWB)
- releaseObject(xlSheet)
- End Sub
Expand|Select|Wrap|Line Numbers
- 'For Each row As Microsoft.Office.Interop.Excel.Worksheet
- 'xlSheet.Cells(1, 1) = "SiteName"
- 'Next
- 'MsgBox(xlSheet.Cells(1, 1).ToString)
- 'Try
- 'Dim MyConnection As System.Data.OleDb.OleDbConnection
- 'Dim DtSet As System.Data.DataSet
- ' Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
- ' MyConnection = New System.Data.OleDb.OleDbConnection("provider = Microsoft.Jet.OLEDB.4.0;"" Data Source = FD.FileName';""Extended Properties = Excel 8.0;")
- ' MyCommand = New System.Data.OleDb.OleDbDataAdapter("SELECT * from [Site Data]", MyConnection)
- ' MyCommand.TableMappings.Add("Table", "TestTable")
- ' DtSet = New System.Data.DataSet
- 'MyCommand.Fill(DtSet)
- 'DataGridView1.DataSource = DtSet.Tables(0)
- 'MyConnection.Close()
- 'Catch ex As Exception
- 'MsgBox(ex.ToString)
- 'End Try