获取的Excel列的最后一个非空单元编程方式 [英] Get Last non Empty Cell of Excel Column Programatically

查看:119
本文介绍了获取的Excel列的最后一个非空单元编程方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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;



我得到的最后一排的最后一行有乙类非空。其中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的产生有一个也许是因为功能相同的结果。幸运的是,这并不有同样的问题,所以如果我使用上述变化1 4中的代码它的工作原理喜欢一个魅力D列。

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:

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列的最后一个非空单元编程方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆