1

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
  • You can do this with the use of the oledb. Here is a link to a demo project which explains it all. or this one with the help of the Interop namespace. Commented Oct 4, 2010 at 12:18

1 Answer 1

10

@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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.?
For that, you need to play with ORDINAL_POSITION from column of row and sort accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.