Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB.NET
Imports System.IO
Imports System.Data
Imports System.Data.OleDb

Public Class Form1

    Private Excel03ConString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'"
    Private Excel07ConString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'"

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Database1DataSet.Table1' table. You can move, or remove it, as needed.
        Me.Table1TableAdapter.Fill(Me.Database1DataSet.Table1)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            OpenFileDialog1.ShowDialog()
        Catch ex As Exception
            MsgBox("error", vbCritical, "my project")
        End Try
    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        Try
            Dim filePath As String = OpenFileDialog1.FileName
            Dim extension As String = Path.GetExtension(filePath)
            Dim header As String = If(checkbox1.Checked, "YES", "NO")
            Dim conStr As String, sheetName As String

            conStr = String.Empty
            Select Case extension

                Case ".xls"
                    'Excel 97-03
                    conStr = String.Format(Excel03ConString, filePath, header)
                    Exit Select

                Case ".xlsx"
                    'Excel 07
                    conStr = String.Format(Excel07ConString, filePath, header)
                    Exit Select
            End Select

            'Get the name of the First Sheet.
            Using con As New OleDbConnection(conStr)
                Using cmd As New OleDbCommand()
                    cmd.Connection = con
                    con.Open()
                    Dim dtExcelSchema As DataTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
                    sheetName = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
                    con.Close()
                End Using
            End Using

            'Read Data from the First Sheet.
            Using con As New OleDbConnection(conStr)
                Using cmd As New OleDbCommand()
                    Using oda As New OleDbDataAdapter()
                        Dim dt As New DataTable()
                        cmd.CommandText = (Convert.ToString("SELECT * From [") & sheetName) + "]"
                        cmd.Connection = con
                        con.Open()
                        oda.SelectCommand = cmd
                        oda.Fill(dt)
                        con.Close()

                        'Populate DataGridView.
                        DataGridView1.DataSource = dt

                        Table1BindingSource.EndEdit()
                        Table1TableAdapter.Update(Database1DataSet.Table1)
                        MsgBox("OK")
                    End Using
                End Using
            End Using
        Catch ex As Exception
            MsgBox("error", vbCritical, "My project")
        End Try
    End Sub
End Class


What I have tried:

This code is functional but I want to import data from excel in ACCESS and no from excel to only datagridview.
In moment the import data I show data in datagridview and when I close this project, in access database there is no new data from import.

Please help? thanks! :)
Posted
Comments
Richard MacCutchan 11-Apr-16 10:48am    
Where is the code that writes the data to Access?
ionMEMBER 11-Apr-16 12:56pm    
In openfiledialog1.. I want to import data from excel to access but this code imports from excel to datagridview1 and no in access. You have any idea?
Richard MacCutchan 12-Apr-16 3:23am    
It has nothing top do with OpenFileDialog, that just gets the filename from the user. You canread the Excel data direct via OLEDB, no need to load it into a DataGridView. And then write it to the access database via ADO.NET. See http://www.codeproject.com/Articles/37055/Working-with-MS-Excel-xls-xlsx-Using-MDAC-and-Oled and http://www.codeproject.com/Articles/8477/Using-ADO-NET-for-beginners.
Sergey Alexandrovich Kryukov 11-Apr-16 11:00am    
Why do you write code showing the grid view? Perhaps you have to confirm the data... or what?
There are a lot of answers around on how to work on Access, there are different API...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900