I am trying to import an excel sheet as a sql table(with column names already in the excel sheet) I am doing the following steps to finally create table.
step 1: browse for excel file and fill the combo box with the list of excel sheets.
Private Sub btnspibrowse_Click(sender As System.Object, e As System.EventArgs) Handles btnspibrowse.Click
If openfile.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Cursor = Cursors.WaitCursor
'Populating the Combobox with the sheet names
xlconnstring = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + openfile.FileName + "; Extended Properties='Excel 12.0 Xml;HDR=YES'")
Dim shtname As String = ""
xldatatable = New DataTable
Try 'for XL oledb
'get the nams of sheets
xlconnstring.Open()
'help regarding getoledbschematale
'https://support.microsoft.com/en-in/kb/309488
'The OLE DB .NET Data Provider uses the GetOleDbSchemaTable method of the OleDbConnection object to expose schema information.
'GetOleDbSchemaTable returns a DataTable that is populated with the schema information.
'The first argument of GetOleDbSchemaTable is the schema parameter,
'an OleDbSchemaGuid argument that identifies which schema information to return (such as tables, columns, and primary keys).
'The second argument is an Object array of restrictions to filter the rows that are returned in the schema DataTable
'(for example, you may specify restrictions for table name, type, owner, and /or schema).
xldatatable = xlconnstring.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim int As Integer = 0
For int = 0 To xldatatable.Rows.Count - 1
If xldatatable.Rows(int)!TABLE_TYPE.ToString = "TABLE" Then
cbodbsheet.Items.Add(xldatatable.Rows(int)!TABLE_NAME.ToString())
End If
Next
xldatatable = Nothing
xlconnstring.Close()
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical, "Database Import Error")
End Try
End If
Cursor = Cursors.Default
End Sub
Step 2: I select the sheet required and show a glimpse of the sheet to user in datagridview
Private Sub cbodbsheet_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbodbsheet.SelectedIndexChanged
Cursor = Cursors.WaitCursor
xlconnstring = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + openfile.FileName + "; Extended Properties='Excel 12.0 Xml;HDR=YES'")
Dim shtname As String = ""
Try 'for XL oledb
xlcommand = New OleDbCommand
xldatatable = New DataTable
'get the nams of sheets
xlconnstring.Open()
Try
'help regarding getoledbschematale
'https://support.microsoft.com/en-in/kb/309488
'The OLE DB .NET Data Provider uses the GetOleDbSchemaTable method of the OleDbConnection object to expose schema information.
'GetOleDbSchemaTable returns a DataTable that is populated with the schema information.
'The first argument of GetOleDbSchemaTable is the schema parameter,
'an OleDbSchemaGuid argument that identifies which schema information to return (such as tables, columns, and primary keys).
'The second argument is an Object array of restrictions to filter the rows that are returned in the schema DataTable
'(for example, you may specify restrictions for table name, type, owner, and /or schema).
xldatatable = xlconnstring.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
'Dim shtindex As Integer = xlschema.Rows.IndexOf(cbodbsheet.SelectedValue)
shtname = xldatatable.Rows(cbodbsheet.SelectedIndex)("TABLE_NAME").ToString()
xlconnstring.Close()
xldatatable = Nothing
Catch ex1 As Exception
MsgBox(ex1, MsgBoxStyle.Critical, "Import Wizard-Excel Table Schema")
End Try
'read from selected sheet
xlconnstring.Open()
Try
xldatatable = New DataTable
xlcommand.CommandText = "SELECT * From [" & shtname & "]"
xlcommand.Connection = xlconnstring
xldataadapter.SelectCommand = xlcommand
xldataadapter.Fill(xldatatable)
xlconnstring.Close()
Catch ex2 As Exception
MsgBox(ex2.ToString(), MsgBoxStyle.Critical, "Import Wizard-Excel Command Query")
End Try
'bind data to gridview
dgxlview.DataSource = xldatatable
Catch ex3 As Exception
MsgBox(ex3.ToString(), MsgBoxStyle.Critical, "Import Wizard-OLEDB Connection Problems")
End Try
spiselectedflag = True
' MsgBox(spiselectedflag)
Cursor = Cursors.Default
xldatatable = Nothing
xlcommand = Nothing
xldataadapter = Nothing
End Sub
Step 3: The selected sheet has a table of any number of rows(it according to requirement). So upon clicking a button I need to first create a table with all the columns of this selected excel sheet. But I am not able to fetch the columns. Kindly guide me with this simple thing.
Thanks for reading.