vb.net 如何获取当前单元格的列索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13626806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:22:44  来源:igfitidea点击:

How to get the column index of the current cell?

c#vb.netexcelepplus

提问by Shane LeBlanc

I have this code here...

我这里有这个代码...

For Each cell In worksheet.Cells(8, 2, lastRow, lastCol)
    Select Case "What Goes Here"
    End Select
Next

I want to verify the column that the current cell is in. I can use...

我想验证当前单元格所在的列。我可以使用...

cell.Address

but that returns a string with both the column letter and row. Would there be another way of getting maybe just the index or letter only of the column or would I just have to do something like....

但这会返回一个包含列字母和行的字符串。是否有另一种方法可以只获取列的索引或字母,或者我只需要做类似的事情......

If cell.Address.Contains("A")

回答by Tim Schmelter

You get it via cell.Start.Column:

您可以通过cell.Start.Column以下方式获得:

int colIndex = cell.Start.Column;

Startis always the same as Endwhen you use foreach.

Start始终与End您使用时相同foreach

The same if you want the row-number instead:

如果您想要行号,则相同:

int rowNum = cell.Start.Row;

Note that the column- and rows are not zero based indices but 1-based. So the first column of the first row has Row=1and Column=1.

请注意,列和行不是基于零的索引,而是基于 1 的索引。所以第一行的第一列有Row=1Column=1