Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用c从excel中的columnHeaderNames中获取行单元格值_C#_Excel_Interop_Excel 2010 - Fatal编程技术网

C# 如何使用c从excel中的columnHeaderNames中获取行单元格值

C# 如何使用c从excel中的columnHeaderNames中获取行单元格值,c#,excel,interop,excel-2010,C#,Excel,Interop,Excel 2010,我的要求是从excel工作表中读取指定列的行。乙二醇 从上面的excel中,如果我需要获取列ie B的行值,如何获取它。 我使用下面的代码获取列标题名称 Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(FileNameTextBox.Text); Excel.Worksheet xlWorksheet =

我的要求是从excel工作表中读取指定列的行。乙二醇

从上面的excel中,如果我需要获取列ie B的行值,如何获取它。 我使用下面的代码获取列标题名称

     Excel.Application xlApp = new Excel.Application();
        Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(FileNameTextBox.Text);
        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);
                }
            }
        }

其中,它给我一个包含a、B、C、D、E、F的列表输出。如果我需要单独获取列C的值,那么如何获取它?

HRD在您的连接中应该是“否”以访问标题数据,然后使用第一个循环中的行和第一个循环中的内部循环来访问行的单元格

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=No;IMEX=2\"";

此方法返回excel工作表第一行中的单元格值:

public static string[] FirstRowInWorkSheet(string filename, int sheetNumber)
    {
        Application xlsApp = new Application();
        Workbook wb = xlsApp.Workbooks.Open(filename,
            0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true);

        var sheets= wb.Worksheets;
        Worksheet xlWorksheet =sheets[sheetNumber]; 

        int columnCount = xlWorksheet.UsedRange.Columns.Count;
        Range xlRange = xlWorksheet.UsedRange;
        string[] firstRow = new string[columnCount];

        for (int c = 0; c < columnCount; c++)
        {
            if (xlRange.Cells[1, c + 1].Value2 != null)
            {
                firstRow[c] = xlRange.Cells[1, c + 1].Value2.ToString();
            }
        }
        return firstRow;
    }
这段代码帮助您以数组的形式获取表的标题。当用户选择列时,获取其索引,然后对一列的单元格进行操作(与对第一行的操作方式相同),只需更改您在单元格[i,j]中所做的修改即可:根据用户的选择修改j