Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My project requires me to read excel data using c#. When i display the worksheet using the gridview, i found that the display also include F1, F2, ... as the header for every column. How i can remove the F1, F2, ...? I want the header of every column is the same as in excel file. Such as, File size, Audio and ...

What I have tried:

Here is my code for connection:
public DataTable ReadExcel(string fileName, string fileExt)
{
string conn = string.Empty;
DataTable dtexcel = new DataTable();
if (fileExt.CompareTo(".xls") == 0)
conn = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties='Excel 8.0;HRD=Yes;IMEX=1';"; //for below excel 2007
else
conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=NO';"; //for above excel 2007
using (OleDbConnection con = new OleDbConnection(conn))
{
try
{
OleDbDataAdapter oleAdpt = new OleDbDataAdapter("select * from [Files Audio$]", con); //here we read data from sheet1
oleAdpt.Fill(dtexcel); //fill excel data into dataTable
}
catch { }
}
return dtexcel;
}
Posted
Updated 11-Jul-19 5:04am

Specify HDR=YES in the extended properties of the connection string, as explained in Working with MS Excel(xls / xlsx) Using MDAC and Oledb[^].
 
Share this answer
 
Comments
choc000 4-May-17 3:50am    
Whoah! My problem have been solved. Thank Youuuuu.
dataGridView1.ColumnHeadersVisible = false;
 
Share this answer
 
Comments
CHill60 11-Jul-19 11:19am    
If you read the question carefully you will notice that it isn't the dataGridView column headers that were the problem - it was the header row within the Excel spreadsheet being included as part of the data. Solution 1 provided the correct solution - you can tell it's the correct solution because the OP has responded with a message to say it solved the problem. They also Accepted it as the answer to their problem (green header).

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