importing excel to database, user selects the destination of excel file

filistor

Member
Joined
Jun 16, 2004
Messages
8
Programming Experience
3-5
hello! i ve built a program whith sql server, and i want the user to be able to update the database from an excel file. I have read the following code

Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=c:\CAD.xls;Extended Properties=""Excel 8.0""")

but i d'like the user to choose the destination of the excel, via an openfiledialog. Can this be done ? I'd also like the user to be able to export the results of a datagrid in an excel file, but in different collumns, and not the whole dataset, just the datagrid. (i've only been able to export the data in a single column).
Thank you very much for your time
 
I Have Used This Code I Copied From Somewhere

Set some public variables at the top of the form:

VB.NET:
    Public filename As String
    Public DSexcel As System.Data.DataSet
    Public MyCommand As System.Data.OleDb.OleDbDataAdapter
    Public MyConnection As System.Data.OleDb.OleDbConnection
Get the excel file: Must have a "sheet1"

VB.NET:
           If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            filename = OpenFileDialog1.FileName
            If Not (filename Is Nothing) Then
                MyConnection = New System.Data.OleDb.OleDbConnection( _
                      "provider=Microsoft.Jet.OLEDB.4.0; " & _
                      "data source=" & filename & " ; " & _
                      "Extended Properties=Excel 8.0;")
                ' Select the data from Sheet1 of the workbook.
                MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
                      "select * from [Sheet1$]", MyConnection)

                DSexcel = New System.Data.DataSet
                MyCommand.Fill(DSexcel)
                MyConnection.Close()
                RGrid.DataSource = DSexcel.Tables(0)
            End If
        End If


The line you need contains: "data source=" & filename & " ; "
 
Back
Top