简体   繁体   English

如何使用C#Excel“Microsoft.Office.Interop.Excel”来计算一列中非空单元格的数量

[英]How can I use C# Excel “Microsoft.Office.Interop.Excel”, to count the amount of not empty cells in a one columns

So I have this spreadsheet, it has Fname, Lname, Pnumber, Email, Cid. 所以我有这个电子表格,它有Fname,Lname,Pnumber,Email,Cid。 Cid is the only really important one, the rest are optional technically. Cid是唯一非常重要的一个,其余的在技术上是可选的。

So instead of just doing a loop to see when the cell values starts being null, I know you can use the 因此,我不知道只是做一个循环来查看单元格值何时开始为空,我知道你可以使用

WorksheetFunction.CountA(some 30 different variable that I do not understand but are optional too); WorksheetFunction.CountA(我不理解的30个不同的变量,但也是可选的);

to get the actual numbers of filled cells, and it is instant. 得到填充细胞的实际数量,它是即时的。 I have no idea how to do this. 我不知道该怎么做。

This is what I'm going to do viva looping, but I know the CountA() function can do it in one line. 这就是我要做的viva循环,但我知道CountA()函数可以在一行中完成。

int i = 1;
while (i <= 200)
{
    cell = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[i, 5];
    if (cell.Value2 > 0)
    {i++;}
    else
    {break;}
}
int totalCol = i;

Ok, this is what I have now, thanks everyone. 好的,这就是我现在所拥有的,谢谢大家。

var cell = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, 1];
    cell.Formula = "=CountA(E1:E200)";
                    double totalCells = (cell.Value2) -1;

I'm not in the IDE right now, but this should do the trick: 我现在不在IDE中,但这应该可以解决问题:

    Dim cl As Microsoft.Office.Interop.Excel.Range
    cl.Formula = "=CountA(E1:E200)"
    myVal = cl.Value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM