Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
Please Help, I have try below code to import an excel files to datagridview. It's work if excel file open by me. If excel file not open, datagridview return blank page, no header, cell, or rows. So to make it code works, I must open the excel file by office and running the application. Where is wrong?

Here's code :
VB
Imports System.Data.OleDb
Public Class Form1
    Dim conn As OleDbConnection
    Dim da As OleDbDataAdapter
    Dim ds As New DataSet
    Dim cmd As OleDbCommand
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        On Error Resume Next
        OpenFileDialog1.Filter = "(*.xls)|*.xls|(*.xlsx)|*.xlsx|All files (*.*)|*.*"
        OpenFileDialog1.ShowDialog()
        conn = New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;" & "data source='" & OpenFileDialog1.FileName & "';Extended Properties=Excel 8.0;")
        da = New OleDbDataAdapter("select * from [Sheet1$]", conn)
        conn.Open()
        ds.Clear()
        da.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)
        TextBox1.Text = OpenFileDialog1.FileName.ToString
        conn.Close()
    End Sub
End Class


thanks, any help are appreciated :)
Posted
Updated 14-Feb-20 2:07am
Comments
What is "open by me"?
sadam.cof 21-Jan-15 5:32am    
Hi tadit, thanks for respond..
I mean "open by me" is a opened an excel file using office and I run my app, then import the excel file Like this http://i58.tinypic.com/121sbqe.jpg . If I just import excel file without opening the excel file on ms. office it just blank datagridview in my app Like this http://i61.tinypic.com/16780av.jpg . please help..
sorry for my bad english..
very thanks.

1 solution

Solved,
Wrong in Connectionstring, the provider is to old an excel 2010. provider=Microsoft.Jet.OLEDB.4.0; it's must be Provider=Microsoft.ACE.OLEDB.12.0
And it works with code :
VB
Imports System.Data.OleDb
Public Class Form1
    Dim conn As OleDbConnection
    Dim da As OleDbDataAdapter
    Dim ds As New DataSet
    Dim cmd As OleDbCommand
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        On Error Resume Next
        OpenFileDialog1.Filter = "(*.xls)|*.xls|(*.xlsx)|*.xlsx|All files (*.*)|*.*"
        OpenFileDialog1.ShowDialog()
        conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & "data source='" & OpenFileDialog1.FileName & "';Extended Properties=Excel 8.0;")
        da = New OleDbDataAdapter("select * from [Sheet1$]", conn)
        conn.Open()
        ds.Clear()
        da.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)
        TextBox1.Text = OpenFileDialog1.FileName.ToString
        conn.Close()
    End Sub


Thanks CodeProject! :)
 
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