Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12201

Get column names from excel file of a specific sheet using c# with OleDbConnection

$
0
0

So far I have managed to get the column names of the whole excel file, but what I would like to do is to get the column names of the excel file of a given table (sheet). How could I modify the code to achieve this. I have been trying for a while now with no positive results, any help much appreciated.

public static List<String> ReadSpecificTableColumns(string filePath, string sheetName)    {        var columnList = new List<string>();        try        {            var excelConnection = new OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='"+ filePath +"';Extended Properties='Excel 12.0;IMEX=1'");            excelConnection.Open();            var columns = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, null);            excelConnection.Close();            if (columns != null)            {                columnList.AddRange(from DataRow column in columns.Rows select column["Column_name"].ToString());            }        }        catch (Exception exception)        {            Console.WriteLine(exception.Message);        }        return columnList;    }

Viewing all articles
Browse latest Browse all 12201

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>