Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.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_Linq_Openxml - Fatal编程技术网

C# 如何获取特定列中的所有单元格?

C# 如何获取特定列中的所有单元格?,c#,excel,linq,openxml,C#,Excel,Linq,Openxml,我有一个程序,从细胞中获取数据,然后将这些信息记录在单独的应用程序中。以下是单元格提取的代码片段。 TableStructure tableStructure = new tableStructure(); tableStructure.TableCols = {"A","B","C","D","E","F","G","H","I","J","K","L","M"}; tableStructure.StartRow = 1; tableStructure.EndRow = 4001; forea

我有一个程序,从细胞中获取数据,然后将这些信息记录在单独的应用程序中。以下是单元格提取的代码片段。

TableStructure tableStructure = new tableStructure();
tableStructure.TableCols = {"A","B","C","D","E","F","G","H","I","J","K","L","M"};
tableStructure.StartRow = 1;
tableStructure.EndRow = 4001;
foreach (var colName in tableStructure.TableCols)
{
    for (var ci = tableStructure.StartRow; ci <= tableStructure.EndRow; ci++)
    {
        var addressName = colName + ci;
        Cell currentCell = wsPart.Worksheet.Descendants<Cell>().Where(c => c.CellReference == addressName).FirstOrDefault();//52000 times
        //some other scripts
    }
}
TableStructure TableStructure=new TableStructure();
tableStructure.TableCols={“A”、“B”、“C”、“D”、“E”、“F”、“G”、“H”、“I”、“J”、“K”、“L”、“M”};
tableStructure.StartRow=1;
tableStructure.EndRow=4001;
foreach(tableStructure.TableCols中的var colName)
{
for(var ci=tableStructure.StartRow;ci c.CellReference==addressName).FirstOrDefault();//52000次
//其他脚本
}
}

通过这段代码,我可以获得所需的输出,但存在性能问题。对于上面的例子,只需要大约9分钟就可以得到电池。如果我评论下一行,只需50秒。
Cell-currentCell=wsPart.sheet.subjects()。其中(c=>c.CellReference==addressName).FirstOrDefault()
我想优化我的代码,就像有一个LINQ一样,通过它我可以获得所有的单元格
subjects().FirstOrDefault(p=>p.RowIndex==RowIndex)

现在我的问题是,有没有一种像上面那样的方法可以获取特定列的所有单元格。

试试这个:

IEnumerable<Cell> cells = wsPart.Worksheet.Descendants<Cell>().Where(c => string.Compare(GetColumnName(c.CellReference.Value),
addressName, true) == 0).OrderBy(r => GetRowIndex(r.CellReference));

foreach (var cell in cells)
{

}
IEnumerable cells=wsPart.Worksheet.subjects()。其中(c=>string.Compare(GetColumnName(c.CellReference.Value)),
addressName,true)==0.OrderBy(r=>GetRowIndex(r.CellReference));
foreach(单元格中的var单元格)
{
}

这是一个优秀的c#吗?显示加载excel文件的代码。thakns,@JamesDev我正在使用open xml读取文件。GetRowIndex(r.CellReference))此函数在哪里?你是从这个链接复制的吗?如果我是正确的,那么我已经试过了,并且有同样的效果。你的意思是它仍然很慢?