Click here to Skip to main content
16,022,417 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey i want to know how to connect the excel 2007 to the c#
please help
Posted
Updated 21-Jul-10 0:36am
v2

Hi,

Follow the below steps
1. Create Excel say TEST.xls in c:\ with some header and rows and columns data in it.
2. Create "Named Object" in excel by select the data in your excel workbook ( select header and data) and then go to menu Insert --> Names --> Define. Name your object and press "Add" Now you have an named object which can be used to read by ADO.NET
3. Below is the code to connect to workbook also to bind to datagridview :)
4. TEST is your "Named Object" in excel use it as table to get data

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties=Excel 8.0");
            OleDbDataAdapter da = new OleDbDataAdapter("select * from TEST", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
 
Share this answer
 
v2
Here[^] is a useful CodeProject article for you. And for connection strings check out this[^]. :)
 
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