简体   繁体   中英

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. Cid is the only really important one, the rest are optional technically.

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);

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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