Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am little bit confusing to save the different name excel file .......I want to import data from excel file in vb.net and save in the database ....I am import my excel file and save data too also.But my problem is that i don't want to fix excel file name or sheet name .Please someone help me

my code is there:
VB
Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
    If validate_data() Then
        Dim MyConnection As System.Data.OleDb.OleDbConnection
        Dim DtSet As System.Data.DataSet
        Dim MyCom As System.Data.OleDb.OleDbDataAdapter

        Dim fileBrowse As New OpenFileDialog
        With fileBrowse
            .Filter = "Excel files(*.xlsx)|*.xlsx|All files (*.*)|*.*"
            .FilterIndex = 1
            .Title = "Import data from Excel file"
        End With
        If fileBrowse.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim fname As String
            fname = fileBrowse.FileName
            DtSet = New System.Data.DataSet
            MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" & fname & " '; " & "Extended Properties=Excel 8.0;")



            MyCom = New System.Data.OleDb.OleDbDataAdapter("select * from [stulist.xlsx$]", MyConnection)
            MyCom.TableMappings.Add("Table", "Student_Master")
            DtSet = New System.Data.DataSet
            MyCom.Fill(DtSet)
            MyConnection.Close()
            For Each row As DataRow In DtSet.Tables(0).Rows
                obj.InsertData("INSERT INTO Student_Master(Student_Name, Student_Rollno, Student_Enrollment) VALUES ('" & row(0).ToString & "','" & row(1).ToString & "','" & row(2).ToString & "')")
            Next
            MsgBox("Imported Item Successfully Saved")

        End If
    End If
End Sub

and in this program i have fix excel file name in the name of "stulist"but actually i dont want to fix excel file name
Posted
Comments
S Houghtelin 21-Feb-14 7:34am    
Don't want to be rude, but I don't understand what you mean by "fix", are you saying that you don't want to create a duplicate file?

2 notes:
1) bad connection string:
VB
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" & fname & " '; " & "Extended Properties=Excel 8.0;")

Please, refer this: http://www.connectionstrings.com/excel/[^]
If you would like to connect to MS Excel 2007 (and higher) file, please use:
CSS
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx; Extended Properties="Excel 12.0 Xml;HDR=YES";

For MS Excel 97-2003, please use:
CSS
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls; Extended Properties="Excel 8.0;HDR=Yes;


2) bad command:
CSS
select * from [stulist.xlsx$]

Your command should refer to existing sheet:
CSS
select * from [Sheet1$]


So, if you would like to dynamically connect to any Excel file, you should list all sheets in a workbook[^] first.

By The Way: have a look at my tip: How to: Get Data from Multiple Workbooks using One OleDbConnection?[^]
 
Share this answer
 
v2
VB
Private Sub btnbrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbrowse.Click
        Txtnumber.Text = ""

        Dim fBrowse As New OpenFileDialog
        With fBrowse
            .InitialDirectory = "c:\"
            .Filter = "Excel files(*.xlsx)|*.xlsx|All files (*.*)|*.*"
            .FilterIndex = 1
            .Title = "Import data from Excel file"

            Dim answ = .ShowDialog

            If answ = DialogResult.OK Then 'if answer not cancel, etc..
                path = System.IO.Path.GetFullPath(fBrowse.FileName) 'picterebox imagelocation = dlg_openfile.filename
            End If
        End With
    End Sub
 
Share this answer
 
Please try this..

VB
MyCom = New System.Data.OleDb.OleDbDataAdapter("select * from [" & fname & "]", MyConnection)
 
Share this answer
 

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