bueno mi problema es que estoy haciendo un proyecto en vb.net 2008 pero quisiera saber ¿como puedo obtener un rango de una celda que esta combinada de excel (celda activa) a vb.net 2008?
este es el código que tengo:
Option Explicit On
Option Strict On
Public Class Form1
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Button1.Text = "Cargar"
txtRange.Text = "A1:C15"
End Sub
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' // Pasar valores para Leer el rango
loadRange("c:\book1.xls", "sheet1", txtRange.Text, DataGridView1)
End Sub
' ----------------------------------------------------------------------------------
' // Subrutina para conectar al libro Excel y obtener el rango de datos
' ----------------------------------------------------------------------------------
Private Sub loadRange( _
ByVal sFileName As String, _
ByVal sSheetName As String, _
ByVal sRange As String, _
ByVal dv As DataGridView)
Try
' // Comprobar que el archivo Excel existe
If System.IO.File.Exists(sFileName) Then
Dim objDataSet As System.Data.DataSet
Dim objDataAdapter As System.Data.OleDb.OleDbDataAdapter
' // Declarar la Cadena de conexión
Dim sCs As String = "provider=Microsoft.Jet.OLEDB.4.0; " & "data source=" & sFileName & "; Extended Properties=Excel 8.0;"
Dim objOleConnection As System.Data.OleDb.OleDbConnection
objOleConnection = New System.Data.OleDb.OleDbConnection(sCs)
' // Declarar la consulta SQL que indica el libro y el rango de la hoja
Dim sSql As String = "select * from " & "[" & sSheetName & "$" & sRange & "]"
' // Obtener los datos
objDataAdapter = New System.Data.OleDb.OleDbDataAdapter(sSql, objOleConnection)
' // Crear DataSet y llenarlo
objDataSet = New System.Data.DataSet
objDataAdapter.Fill(objDataSet)
' // Cerrar la conexión
objOleConnection.Close()
' // Enlazar DataGrid al Dataset
With dv
.DataSource = objDataSet
.DataMember = objDataSet.Tables(0).TableName
End With
Else
MsgBox("No se ha encontrado el archivo: " & sFileName, MsgBoxStyle.Exclamation)
End If
Exit Sub
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
End Class
espero me puedan ayudar ..... saludos
