In VSTO Excel, how to detect data in cells?

后端 未结 5 1530
粉色の甜心
粉色の甜心 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:08

    This should be pretty fast:

        private void CheckForContent()
        {
            Worksheet activeSheet = ActiveSheet;
            var range = activeSheet.get_Range("A1", GetExcelColumnName(activeSheet.Columns.Count)+activeSheet.Rows.Count.ToString() );
            range.Select();
            range.Copy();
            string text = Clipboard.GetText().Trim();
            if(string.IsNullOrEmpty(text))
            {
                MessageBox.Show("No text");
            }
        }
    
        private string GetExcelColumnName(int columnNumber)
        {
            int dividend = columnNumber;
            string columnName = String.Empty;
            int modulo;
    
            while (dividend > 0)
            {
                modulo = (dividend - 1) % 26;
                columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
                dividend = (int)((dividend - modulo) / 26);
            }
            return columnName;
        }
    

提交回复
热议问题