Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need import a column from excel to datagrid and also checking the column header values
Posted

1 solution

Hi ,
Try this
C#
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\Book1.xls;Extended Properties=\"Excel 8.0;Readonly=False;IMEX=0;\"";


            using (OleDbConnection objConn = new OleDbConnection(sConnectionString))
            {
                objConn.Open();
                using (OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet$]", objConn))
                {
                    objCmdSelect.CommandType = CommandType.Text;
                    OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(objCmdSelect);

                    DataSet objDataset1 = new DataSet();

                    objAdapter1.Fill(objDataset1);
                   dataGridView1.DataSource = objDataset1.Tables[0];
//Bring all the Columns then show only what you need 
                   dataGridView1.Columns[1].Visible = false ;
                }

            }

Best Regards
M.Mitwalli
 
Share this answer
 
Comments
P.Vinoth 20-Jun-12 2:50am    
Thanks for your answer. i am using sql database and i need to import the data from excel to sql after checking that two column names are same. please post your answer quickly

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