In VSTO Excel, how to detect data in cells?

后端 未结 5 1531
粉色の甜心
粉色の甜心 2020-12-19 07:38

A process to quickly detect whether there is data in a given worksheet or not, without actually looping through all of the rows/columns of the worksheet.

For my curre

5条回答
  •  时光取名叫无心
    2020-12-19 08:05

    I found the following solution, which is also instantaneous but I'm not sure how accurate it is... it has passed all my tests thus far.

    Here it is for anyone who wants to know:

    Worksheet sheet = (Worksheet)this.Application.ActiveSheet;
    Range usedRange = sheet.UsedRange;
    bool isUsed = (usedRange.Count > 1);
    if (usedRange.Count == 1)
    {
      isUsed = (usedRange.Value2 != null) &&
               (!string.IsNullOrEmpty(usedRange.Value2.ToString()));
    }
    
    if(isUsed)
    {
      // worksheet cells not empty
    }
    

    I suppose this is a lot simpler than blowing up the clipboard every time I do the check or counting all the non-empty cells in the worksheet. Thanks Mikael and Mike, I appreciate both your answers.

提交回复
热议问题