开发者

read column names from excel file in c#

开发者 https://www.devze.com 2023-01-18 11:58 出处:网络
I have to implement a file upload feature in which users are allowed to upload files containing tabular data. On uplo开发者_开发知识库ading the file I want to find the column names of table. How can I

I have to implement a file upload feature in which users are allowed to upload files containing tabular data. On uplo开发者_开发知识库ading the file I want to find the column names of table. How can I do this?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号