I have to implement a file upload feature in which users are allowed to upload files containing tabular data. On uploading the file I want to find the column names of table. How can I do this?
1 Answer
@vc 74 I would like to point out some mistake in code:
Instead of having sheetColumns.Rows, there should be sheetColumns.Columns as it was already referencing to DataColumn type.
To read all the column names existing in particular sheet of excel file, DataRow should be referenced as below:
After opening the connection, code goes like this:
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[]
{ null,null, sheetName, null });
List<string> listColumn = new List<string>();
foreach (DataRow row in dt.Rows)
{
listColumn.Add(row["Column_name"].ToString());
}
listColumn contains the column names existing in the specified sheet.
2 Comments
arpan shah
I did the same thing but its giving me the column name in Alphabetical order. Do you know how can i get the column name in the same order it is the file.?