Microsoft Interop: Excel Column Names

39,167
     Excel.Application xlApp = new Excel.Application();
     Excel.Workbook xlWorkbook = xlApp.Workbooks.Open("workbookname");
     Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1]; // assume it is the first sheet
     int columnCount = xlWorksheet.UsedRange.Columns.Count;
     List<string> columnNames = new List<string>();
     for (int c = 1; c < columnCount; c++)
     {
         if (xlWorksheet.Cells[1, c].Value2 != null)
         {
             string columnName = xlWorksheet.Columns[c].Address;
             Regex reg = new Regex(@"(\$)(\w*):");
             if (reg.IsMatch(columnName))
             {
                 Match match = reg.Match(columnName);             
                 columnNames.Add(match.Groups[2].Value);
             }                      
        }
     }

This will put each column name in a List<string> which you can then bind to a drop down box.

Share:
39,167
Priyank Thakkar
Author by

Priyank Thakkar

• 9 years of experience in developing &amp; designing thin client and thick client applications using Java, Spring MVC, Hibernate, HTML, CSS3, jQuery, JavaScript, Eclipse Rich Client Platform, C#, .net framework, Windows Presentation Foundation (WPF) • Hands on experience on front-end UI frameworks like AngularJS, Foundation 6 and added skills like NodeJS and ExpressJS via web based trainings • Mentored newly inducted members of the team by providing them product knowledge &amp; provided technical trainings and grooming • Familiar with Waterfall and Agile methodologies. • Have been part of requirement brainstorming, design and development of applications, end to end development • Conduct technical interviews and get new developers for the team • Having passion for learning new things and enjoys a variety of work - ambitious and inquisitive

Updated on August 15, 2020

Comments

  • Priyank Thakkar
    Priyank Thakkar over 3 years

    I am using Microsoft Interop to read the data.

    In excel-sheet the column-names are like A,B,C,D,....,AA,AB,.... and so on. Is there any way to read this column-names?

    If you need any other info please let me know.

    Regards, Priyank

  • squelos
    squelos about 12 years
    Where do you find the doc concerning the whole interop.excel namespace ? By looking at the MSDN, I cant seem to find anything interesting.
  • Jetti
    Jetti about 12 years
    @squelos you can find some information from MSDN here. Other info I got from a book. I also have a blog post which you can read here that deals with reading Excel data with C# and .NET 4.0
  • Jetti
    Jetti about 12 years
    @PriyankThakkar I'm glad I could help
  • Rob Koch
    Rob Koch almost 11 years
    good starting point for me! I had wanted to copy the column names from Excel over to a CSV file and adjusted your code to columnNames.Add(xlWorksheet.Cells[1, c].Value); Thanks!