Jump to content
rs8cap

Importar dados excel 2010 para datagrid VB 2008

Recommended Posts

rs8cap

Boa tarde, necessito de criar um procedimento de forma a extrair informação de uma folha de excel para um datagrid de VB 2008 mas coloco este código e dá sempre um erro. Alguém me pode dizer porquê?

o código que utilizo é este:

Imports System.Data.OleDb

Public Class Fecho_Mes

Private da As OleDbDataAdapter

Private dt As DataTable

Dim conexao_Excel As String = "Provider=Microsoft.Jet.OleDb.4.0;data source=c:\Users\ricardo.pedro\Downloads\HF_ AO a 201306.xls;Extended Properties=Excel 14.0;"

Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click

Dim da as String = Nothing

' cria um novo dataadapter

da = New OleDbDataAdapter("SELECT * FROM [HF_ AO a 201306$]", conexao_Excel)

'

' preenche a tabela com dados da planilha Excel

dt = New DataTable

da.Fill(dt)

' define a chave primaria

dt.PrimaryKey = New DataColumn() {dt.Columns(0)}

' registros somente podem ser inseridos usando esta tecnica

dt.DefaultView.AllowDelete = False

dt.DefaultView.AllowEdit = True

dt.DefaultView.AllowNew = True

' vincula o modo de visao padrão da tabela ao datagrid

DataGridView1.DataSource = dt.DefaultView

End Sub

End Class

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.