पाशन से बचने और लगभग तात्कालिक निष्पादन की गति का लाभ लेने के लिए, आपको Excel.WorksheetFunction.CountA
विधि है, जिसमें = COUNTA() कार्यपत्रक समारोह के रूप में एक ही परिणाम देता है का उपयोग कर सकते हैं।
यह मानते हुए कि आपके Excel.Application संदर्भ 'excelApp', आप में निम्नलिखित सी # 4.0 की तरह कोड का उपयोग कर सकते के नाम पर है और अपने Excel.Worksheet संदर्भ 'वर्कशीट' नाम दिया गया है:
// 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.
}
सी # में 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.
}
मुझे लगता है कि यह आपके लिए यह करना चाहिए!
माइक
मैं जानता हूँ कि यह पुराना है, लेकिन उत्तर के अलावा इन घटनाओं बंद कर देते हैं, वर्कशीट पॉप्युलेट, तो उन्हें फिर से चालू करते: 'currentInstance.EnableEvents = false/सच; वर्तमानInstance.ScreenUpdating = false/true; ' –