Excel to VB.net to Access HELP!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ISATownsME
    New Member
    • Oct 2008
    • 3

    Excel to VB.net to Access HELP!!

    HELLO world, this is my first post and heres my problem.

    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

    Code:
    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
    Heres another failed approach
    Code:
    '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
  • joedeene
    Contributor
    • Jul 2008
    • 579

    #2
    Would this help maybe? If not, have you tried Google?

    joedeene

    Comment

    Working...