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

Is it possible to read data from excel with the excel headers.
i.e In Excel, we are having frozen row(A,B,C..) and columns(1,2,3,4).

Now I can read the excel the data and bind with datagrid. But the frozen columns are generating with F1,F2..

I want to read the data from excel along with excel headers directly to bind with data gridview.

Pl anyone help me in this??

Thanks in advance.

Regards,
Mohana.
Posted

Hi Mohana,
Following is the code-



C#
OleDbConnection theConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=book1.xls;Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;\"");
theConnection.Open();
OleDbDataAdapter theDataAdapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", theConnection);
DataSet theDS = new DataSet();
DataTable dt = new DataTable();
theDataAdapter.Fill(dt);
this.dataGridView1.DataSource = dt.DefaultView;
 
Share this answer
 
Comments
Mohanapriya Ranganathan 28-May-12 7:26am    
I want to read the excel contents with the Default Excel headers..Rows(A,B,C..AA) and Columns(1,2,3)
Jαved 28-May-12 9:37am    
Set HDR=YES in connection string.
Mohanapriya Ranganathan 29-May-12 1:57am    
HDR=YES sets only the first row of excel to header. Not the excel header(A,B,C..) and columns(1,2,3..)
Jαved 29-May-12 2:56am    
All right. I think your question is answered here-
http://stackoverflow.com/questions/9721161/microsoft-interop-excel-column-names

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