Microsoft Interop:Excel列名称

Pri*_*kar 9 c# office-interop excel-interop

我正在使用Microsoft Interop来读取数据.

在excel-sheet中,列名称类似于A,B,C,D,....,AA,AB,....等等.有没有办法阅读这个列名?

如果您需要任何其他信息,请告诉我.

此致,Priyank

Jet*_*tti 13

     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);
             }                      
        }
     }
Run Code Online (Sandbox Code Playgroud)

这会将每个列名称放在一个List<string>您可以绑定到下拉框的列中.