Click here to Skip to main content
16,020,345 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to get column data from Excel sheet which contains Header row


this is my code

C#
private void button2_Click(object sender, EventArgs e)
        {
            try
            {

                OpenFileDialog openfiledialog1 = new OpenFileDialog();
                openfiledialog1.ShowDialog();
                string filePath = openfiledialog1.FileName;
                string extension = Path.GetExtension(filePath);
                string header = rdbuttonyes.Checked ? "YES" : "NO";
                string conStr, sheetName;

                conStr = string.Empty;
                switch (extension)
                {

                    case ".xls": //Excel 97-03
                        conStr = string.Format(Excel03ConString, filePath, header);
                        break;

                    case ".xlsx": //Excel 07
                        conStr = string.Format(Excel07ConString, filePath, header);
                        break;
                }

                //Get the name of the First Sheet.
                using (OleDbConnection con = new OleDbConnection(conStr))
                {
                    using (OleDbCommand cmd = new OleDbCommand())
                    {
                        cmd.Connection = con;
                        con.Open();
                        DataTable dtExcelSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                        sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                        con.Close();
                    }
                }

                //Read Data from the First Sheet.
                using (OleDbConnection con = new OleDbConnection(conStr))
                {
                    using (OleDbCommand cmd = new OleDbCommand())
                    {
                        using (OleDbDataAdapter oda = new OleDbDataAdapter())
                        {
                            DataTable dt = new DataTable();
                            cmd.CommandText = "SELECT top 1 * From[" + sheetName + "]";



 

                       

                            cmd.Connection = con;
                            con.Open();
                            oda.SelectCommand = cmd;
                            oda.Fill(dt);
                            //dgvExcelResult.SelectedRows.ToString();
                            con.Close();
                            dgvExcelResult.AutoGenerateColumns = true;
                            //Populate DataGridView.
                         

                            dgvExcelResult.DataSource = dt;
                            dgvExcelResult.Visible = true;

                          
                        }
                    }
                }
            }
            catch
            {
               
            }
        }


i want to get column values like, ID, DESCRIPTION,PRODUCT from EXCEl file...i tried giving column header in the Above Query but not binding to datagrid because these columns are header row.
Posted
Updated 12-Mar-15 19:12pm
v3
Comments
Sinisa Hajnal 12-Mar-15 10:26am    
What have you tried? Any code you're already done? What is the problem?
[no name] 12-Mar-15 21:56pm    
Can you show what's the excel like? Any merge?Do you want to get all column data without heading ?The question is not clear.
Member 10371894 13-Mar-15 2:16am    
hi i have modified my question..please help by providing solution
Maciej Los 13-Mar-15 2:50am    
What's error message?

1 solution

I suspect you sql command is wrong:
C#
cmd.CommandText = "SELECT top 1 * From[" + sheetName + "]";

It should be something like:
C#
cmd.CommandText = string.Concat("SELECT top 1 * From [", sheetName, "$]");


Please, see: Accessing Microsoft Office Data from .NET Applications[^]
 
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