我正在使用 Microsoft Interop 来读取数据。
在 Excel 工作表中,列名称类似于 A、B、C、D、....、AA、AB、.... 等等。有什么办法可以读取这个列名吗?
如果您需要任何其他信息,请告诉我。
问候,
普里扬克
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>
然后您可以将其绑定到下拉框。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)