c# interop excel - 如何获取指定单元格值的坐标值

c# interop excel - How to get coordinates value specifing a cell value

有没有办法获取给定单元格值的单元格的坐标?

例如我想找到单词所在的单元格坐标:"General_Assembly"。

你能帮我解决这个问题吗?

如果您知道 table 的尺寸,则可以制作两个简单的循环(针对行和列)并检查每个单元格的文本。

Excel.Worksheet ws = *load your excel sheet from excel file*;
int row = 0;
int column = 0;
int maxRows = *whatever is the height of your table*
int maxColumns = *whatever is the width of your table*
string searchFor = "General_Assembly";

for(int i = 1; i <= maxRows; i++)
    for(int j = 1; j <= maxColumns; j++)
        if((ws.Cells[i, j] as Range).Value.ToString() == searchFor)
        {
            row = i;
            column = j;
        }

这就是您获取行和列索引的方式,以及如何在 matrix/table 个单元格中循环。

您真的不需要知道尺寸,但任何 excel 文件中的最大行数是 1,048,576 行乘以 16,384 列,这会花费太多时间。

此外,正如@Xiaoy312 所建议的那样,您没有付出任何努力就可以自己找到您需要的东西。我没有,但你试试 google-ing "C# Excel interop how to find cell by cell value"。我刚才轻描淡写了,但那是你要求的真实 "help"。我提供了代码,以便您了解如何循环 table/matrix。同样,您可以通过 google-ing "c# how to find cell value in table/matrix" 来学习这个循环。因此,对于您的下一个问题,请参阅任何教程 and/or 在线帮助以了解您要搜索的内容。

如果这回答了您的问题,请将其标记为答案and/or如果这有助于您理解@Xiaoy312 以这种方式发表评论的理由/帮助您学习如何google/帮助您了解如何循环 matrix/table.