Click here to Skip to main content
15,886,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to remove column header of a data table without losing data in asp.net c# ?

I am fetching the data from excel via Oledb to a data table and I am exporting the same to another. I want to remove the column header of the data table without losing column value.

What I have tried:

C#
string filelocation1 = @"D:\Alex\Arab_Test_1.xls";



            string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filelocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            //create connection
            OleDbConnection oleDBConnection = new OleDbConnection(connectionString);
            oleDBConnection.Open();

            //create the adapter with the select to get 
            OleDbDataAdapter adapter = new OleDbDataAdapter("select * from [Sheet1$]", oleDBConnection);

            // Create the dataset and fill it by using the adapter.
            DataTable dataTable = new DataTable();
            adapter.FillSchema(dataTable, SchemaType.Source);
            adapter.Fill(dataTable);
            DataSet ds=new DataSet();
            ds.Tables.Add(dataTable);      

 for (int rowNo = 0; rowNo < ds.Tables[0].Rows.Count; rowNo++)
            {
                for (int colNo = 0; colNo < ds.Tables[0].Columns.Count; colNo++)
                {
                    newCERecord[rowNo] = ds.Tables[0].Rows[rowNo][colNo].ToString();

                }
                //iRow++;
            }
            
           
            // Call update on the adapter to save all the changes to the dataset
            adapter.Update(dataTable);
Posted
v3
Comments
Sinisa Hajnal 4-Nov-16 3:47am    
You could loop through all columns and remove / replace column header text. You cannot remove them completely because headers are datatable object property. It is probably possible with some reflection, but why bother?

If you mean you need to remove header from excel it is much simpler. Just remove first row from the datatable.

1 solution

In the connection string set HDR=No.
Regards.
 
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