以Excel程序方式获取Excel的最后一个非空单元格 [英] Get Last non Empty Cell of Excel Column Programatically
问题描述
我的excel表总是看起来像下面
I have excel sheets that always look like below
A | B | C
| |
| | something here maybe
1 | x | k
2 | y | l
3 | z | n
| j |
| j |
...
| |
我想获取A列中最后一个非空单元格的值。一列始终从A6开始,并且一直持续一段时间,而不是空白,但是列B继续一段时间。这样做的效果是,如果我使用以下代码:
I want to get the value of the last non empty cell in A column. A column always starts at A6 and goes for a while and after than it is empty but column B continues for a while. This has the effect that if I use the code below:
sh = app.Workbooks.get_Item("Workbook1.xlsx").Worksheets.get_Item("Sheet1");
fullRow = sh.Rows.Count;
lastRow = sh.Cells[fullRow, 1].End(Excel.XlDirection.xlUp).Row;
我最后一行得到B的最后一行不为空。在A开始为零的地方,甚至可能是50行。有没有办法使用C#而不迭代所有值?
I get as last row the last row that has B non empty. Which might be even 50 rows below the place where A starts being null. Is there a way to do it with C# without iterating over all values?
编辑
事实证明,列A基于列C我应该检查C.因为事实证明,C也是因为功能而产生与A相同的结果。幸运的是有一个列D没有相同的问题,所以如果我使用上面的代码更改1为4它的作品喜欢一个魅力。
It turns out that Column A is based on column C so I should check for C. As it turns out C is yielding the same result with A maybe because of the function. Luckily there is a column D that does not have the same issue so if I use the code above changing 1 for 4 it works likes a charm.
推荐答案
编辑:
对不起,我最初误解了这个问题,谢谢KekuSemau清算
Sorry, I initially misunderstood the question, thanks KekuSemau for clearing that up!
尝试:
sh.Range["A1", sh.Cells[1,sh.Rows.Count]].End(Excel.XlDirection.xlDown).Value;
另外,如果你知道列中没有空单元格,你可以使用CountA: p>
Also, if you know there will be no empty cells in the column you could use CountA:
var usedRows = xlApp.WorksheetFunction.CountA(xlWorkSheet.Columns[1]);
这篇关于以Excel程序方式获取Excel的最后一个非空单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!