Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all experts,

I want to import all data from my excel to datagridview in vb.net.
if i don't open excel file, it will error " External Table is not in the expected format." but if i open it, it doesn't display error.
VB
Dim MyConnection As OleDb.OleDbConnection,DA As OleDb.OleDbDataAdapter MyConnection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.TextBox1.Text & ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'") 

DA = New System.Data.OleDb.OleDbDataAdapter("select * from [Detail$]", MyConnection) 
DA.TableMappings.Add("Table", "Tbl1") 
DtSet = New System.Data.DataSet 
DA.Fill(DtSet) 
Me.DataGridView1.DataSource = DtSet.Tables(0) 


Does anybody know how can i import data from excel in both cases( Open Excel file and not open excel file)?

Note:
My MS Office is : 2007

Thanks

TONY
Posted
Updated 9-Aug-12 3:48am
v5
Comments
Kenneth Haugland 8-Aug-12 21:42pm    
And the code looks like?
[no name] 8-Aug-12 21:54pm    
Check your connection string and make sure you are using the right one for the version of Excel.
soeun tony 8-Aug-12 22:00pm    
Hi Kenneth,
Sorry that don't input my code. Here is my code:
Dim MyConnection As OleDb.OleDbConnection,DA As OleDb.OleDbDataAdapter
MyConnection = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.TextBox1.Text & ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'")
DA = New System.Data.OleDb.OleDbDataAdapter("select * from [Detail$]", MyConnection)
DA.TableMappings.Add("Table", "Tbl1")
DtSet = New System.Data.DataSet
DA.Fill(DtSet)
Me.DataGridView1.DataSource = DtSet.Tables(0)

Note:
My MS Office is : 2007

Thanks

TONY
Joan M 9-Aug-12 9:51am    
Soeun tony, when you want to add information to your question it is a good idea to add it to the question itself, not only for the CPian that has asked it. doing so you get the benefit of all the rest of the community to see it, added to teh fact that you can tell the CPian that asked it that you've updated your question.
To do so you just have to click the link "Improve question" that's just under the question itself.
soeun tony 9-Aug-12 22:13pm    
Hi Joan,
Thanks for your good idea.i'll follow you next time.

1 solution

Use Microsoft.ACE.OLEDB.12.0 for Excel 2007 http://www.connectionstrings.com/excel-2007[^]
 
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