在VSTO Excel中,如何检测电池的数据? [英] In VSTO Excel, how to detect data in cells?

查看:199
本文介绍了在VSTO Excel中,如何检测电池的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人知道如何快速地检测是否有一个给定的工作表或没有数据,没有经过所有的行实际的循环/工作表的列摸不着头脑。

I was wondering if anyone knew how 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 to figure this out.

我写信,将数据导入正确到活动工作表(如果它没有被修改),或创建一个新的工作表,并导入到它,而不是进口国。我整个板当前循环,并有一些明显的滞后时间在我的导入。

I am writing an importer that will import data right into the active worksheet (if it has not been modified), or create a new worksheet and import into it instead. I am currently looping through the entire sheet, and there is some noticeable lag-time in my import.

我会AP preciate在这个问题上的任何帮助。谢谢!

I would appreciate any help in the matter. Thank you!

推荐答案

要避免循环和采取的几乎是瞬时的执行速度的优势,您可以使用 Excel.WorksheetFunction.CountA 方法,该方法返回相同的结果= COUNTA()工作表函数。

To avoid looping and take advantage of nearly instantaneous execution speed, you can use the Excel.WorksheetFunction.CountA method, which returns the same result as the =CountA() worksheet function.

假设你的Excel.Application引用名为excelApp和你Excel.Worksheet引用名为工作表,可以用code像下面这样在C#4.0:

Assuming that your Excel.Application reference is named 'excelApp' and your Excel.Worksheet reference is named 'worksheet', you can use code like the following in 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以下,这是更详细一点,因为你必须明确地提供缺少的可选参数:

In C# 3.0 and below, it's a bit more verbose, because you have to explicitly provide the missing optional arguments:

// 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.
}

我想这应该为你做!

I think this should do it for you!

迈克

这篇关于在VSTO Excel中,如何检测电池的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆