Click here to Skip to main content
14,449,906 members
Rate this:
Please Sign up or sign in to vote.
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 :
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 4 days ago
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100