一种快速检测给定工作表中是否存在数据的过程,而无需实际遍历工作表中的所有行/列。
对于我的当前流程,我目前正在遍历整个工作表,在导入时有一些明显的延迟时间。
一种快速检测给定工作表中是否存在数据的过程,而无需实际遍历工作表中的所有行/列。
对于我的当前流程,我目前正在遍历整个工作表,在导入时有一些明显的延迟时间。
Excel.WorksheetFunction.CountA
方法,它返回与=CountA()工作表函数相同的结果。
假设你的Excel.Application引用被命名为'excelApp',你的Excel.Worksheet引用被命名为'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及以下版本中,它显得有些冗长,因为你必须明确提供缺失的可选参数:// 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,而且使用非常频繁,所以我希望能够与您分享我学到的东西。
根据您提供的信息,我建议将其转换为对象数组并使用该信息进行操作。基本上,您可以按以下方式访问这些值:
object[,] arrayValues = (object[,])ExcelRange.Value2;
arrayValues是一个2D数组([行,列])。Excel可以非常快地填充数组,并且对数组的操作当然会非常高效(不用担心装箱操作的性能问题,这不是个问题,相信我)。
我找到了下面这个解决方案,它也是瞬间完成的,但我不确定它的准确性,目前它已经通过了我所有的测试。
以下是想要知道的人:
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,我很感激你们的回答。
Application.WorksheetFunction.CountA
方法,你可以在一行中捕获所有数据和仅仅数据,而不是五行。 - Mike Rosenblum怎么样?
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;
}
currentInstance.EnableEvents = false/true; currentInstance.ScreenUpdating = false/true;
- Jeremy Thompson