Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello sir ,

I want to display Excel sheet data in datagridview with sheet name. I have done that but in that also I have to select excel sheet column name like A,B,C,...etc where I can get A-column may be name B-column may be phoneno ,c-column may be address etc .
I have taken two textbox. In one textbox I have to enter column name A(for name) and another textbox I have to enter B(for phone no). I am not getting how to write code for selecting column. Please help me.

Thanks & Regards
Vasu Hajare

THIS IS MY CODE :
string file = textBox1.Text;
string ss = textBox2.Text;
string sss = txtmobile.Text.ToString();
string ExcelConnectionString = ("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + file + "; Extended Properties = \"Excel 8.0;HDR=Yes;IMEX=1\";");
OleDbDataAdapter myCommandExcel = new      OleDbDataAdapter("SELECT " + txtfirstname.Text + "," +sss+ " FROM [" + ss + "$]", ExcelConnectionString);

DataSet myDataSet = new DataSet();
myCommandExcel.Fill(myDataSet,"ExcelInfo" );
dataGridView1.DataSource = myDataSet.Tables[0].DefaultView;
Posted
Updated 21-Dec-10 20:12pm
v2

May this Help[^]
CodeProject[^]
 
Share this answer
 
v3
Try using like this:

C#
using System.Data;
using System.Data.OleDb;
...
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 [ExcelColumName], [ExcelColumName] from MyObject", con);
DataTable dt = new DataTable();
da.Fill(dt);


Replace "ExcelColumName" with excel column name. And remember, square braces will be there.
 
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