Microsoft Interop:Excel列名称 [英] Microsoft Interop: Excel Column Names

查看:122
本文介绍了Microsoft Interop:Excel列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Microsoft Interop to read the data.

在excel工作表中,列名类似A,B,C,D,...,AA,AB等.有什么方法可以读取此列名?

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.

关于, 普里扬克

推荐答案

     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);
             }                      
        }
     }

这会将每个列名放在List<string>中,然后您可以将其绑定到下拉框.

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

这篇关于Microsoft Interop:Excel列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆