read column names from excel file in c#

后端 未结 1 684
不知归路
不知归路 2021-01-16 10:23

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.

相关标签:
1条回答
  • 2021-01-16 11:04

    @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 讨论(0)
提交回复
热议问题