Necesito ayuda para importar datos de ciertas columnas de excel a un datagridview o para que el sistema inserte los datos de excel a una base de datos en SQL Server
Estoy trabajando con Excel 2003, Visual Studio 2005 y Sql Server 2005
Mi archivo xls tiene columnas de la A a la AG pero yo solo quiero datos de las columnas R , O, P, Q y S... y realmente no se como realizar la cadena... lo que tengo hasta ahora es esto:
Código vb:
Ver original
Dim Comm As New SqlCommand AbrirConexion() Dim myStream As IO.Stream = Nothing Dim BuscarArchivo As New OpenFileDialog() BuscarArchivo.InitialDirectory = "Desktop" BuscarArchivo.Filter = "All files (*.*)|*.*" BuscarArchivo.FilterIndex = 2 BuscarArchivo.RestoreDirectory = True If BuscarArchivo.ShowDialog() = System.Windows.Forms.DialogResult.OK Then Try myStream = BuscarArchivo.OpenFile() If (myStream IsNot Nothing) Then '**** IMPORTAR ARCHIVO A SQL **** Dim conexion As New OleDbConnection Dim comando As New OleDbCommand Dim adaptador As New OleDbDataAdapter Dim ds As New DataSet conexion.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & BuscarArchivo.FileName & " ; Extended Properties= Excel 8.0;" conexion.Open() Dim hoja As String = Strings.Left(BuscarArchivo.SafeFileName.ToString, 31) comando.CommandText = "SELECT R,O, P, Q, S " & _ " FROM [" & hoja & "]" comando.Connection = conexion adaptador.SelectCommand = comando adaptador.Fill(ds, "productos") Dim conexion2 As New SqlConnection Dim cmdguardar As New SqlCommand Dim filas As Integer = 0 Dim inserts As Integer = 0 For Each f As DataRow In ds.Tables(0).Rows If filas < ds.Tables(0).Rows.Count - 7 Then Comm.CommandText = "INSERT INTO [NSO].[dbo].[Items]([Categoria],[Cod_IMDB_Items],[Cod_NSO_Items],[Items],[Barcode])" & _ "VALUES('" & f(0).ToString() & " '," & f(1).ToString() & ", " & f(2).ToString() & " ,'" & f(3).ToString() & "','" & f(4).ToString() & "')" Comm.Connection = Cone inserts = inserts + Comm.ExecuteNonQuery() filas = filas + 1 Else End If Next MsgBox("Se cargaron " + inserts.ToString() + " items en la lista de INV") conexion.Close()
la verdad he buscado y no encuentro una solucion... espero me ayuden y gracias a quienes puedan ayudarme