在VSTO Excel中,如何检测单元格中的数据?

我想知道是否有人知道如何快速检测给定的工作表中是否有数据,而不需要实际遍历工作表中的所有行/列来解决这个问题。

我正在编写一个导入程序,将数据直接导入活动工作表(如果尚未修改),或者创build新的工作表并导入到其中。 我目前正在循环浏览整个表格,而且我的导入有一些明显的滞后时间。

我将不胜感激任何帮助。 谢谢!

为避免循环,并利用几乎即时的执行速度,可以使用Excel.WorksheetFunction.CountA方法,该方法返回与= CountA()工作表函数相同的结果。

假设您的Excel.Application引用被命名为“excelApp”,并且您的Excel.Worksheet引用被命名为“工作表”,则可以使用类似于C#4.0中的以下代码:

 // C# 4.0 int dataCount = (int)excelApp.WorksheetFunction.CountA(worksheet.Cells); if (dataCount == 0) { // All cells on the worksheet are empty. } else { // There is at least one cell on the worksheet that has non-empty contents. } 

在C#3.0及更低版本中,它有点冗长,因为你必须明确地提供缺less的可选参数:

 // C# 3.0 and below int dataCount = (int)excelApp.WorksheetFunction.CountA( worksheet.Cells, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); if (dataCount == 0) { // All cells on the worksheet are empty. } else { // There is at least one cell on the worksheet that has non-empty contents. } 

我认为这应该为你做!

麦克风

我在VSTO和Excel工作了一段时间,在一个非常激烈的水平,所以我希望我可以跟你分享我学到的东西。

根据你提供的信息,我build议转换成一个对象数组,然后用这些信息来处理。 基本上你可以访问这些值:

 object[,] arrayValues = (object[,])ExcelRange.Value2; 

arrayValues是一个二维数组([row,column])。 Excel以极快的速度填充数组,当然,对数组的操作将非常高效(不要担心拳击的性能,这不是问题,相信我)。

HTH,James

我发现下面的解决scheme,这也是瞬间的,但我不知道它有多准确…迄今为止它已通过我所有的testing。

这里是任何想要知道的人:

 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 } 

我想这比在每次检查或计算工作表中的所有非空单元时炸毁剪贴板要简单得多。 感谢Mikael和Mike,我很欣赏你的答案。

怎么样?

 public static bool IsSheetEmpty(int sheetNo) { bool isEmpty = false; if (sheetNo <= Globals.ThisAddIn.Application.Worksheets.Count) { Worksheet ws = Globals.ThisAddIn.Application.Worksheets[sheetNo]; if (ws.UsedRange.Address.ToString() == "$A$1" && String.IsNullOrWhiteSpace(ws.get_Range("A1").Value2)) { isEmpty = true; } } else { // or add your own error handling when sheetNo is not found } return isEmpty; } 

示例调用

 bool isFirstEmpty = IsSheetEmpty(1); 

这应该是相当快的:

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