如何检查单元格是否为空(Excel\VisualC#) [英] How to check if a cell is empty (Excel\VisualC#)

查看:539
本文介绍了如何检查单元格是否为空(Excel\VisualC#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目的是检查 Sheet1 中的每一行,以便发现有多少行,所以我放了一个do\while,一旦到达则应该停止空白单元格

My aim is to check line per line in the Sheet1 in order to discover how many rows are, so i put a do\while that should stop once it reaches a blank cell

示例:


行1数据
行2数据
行3数据
row4数据
row5数据

row1 data
row2 data
row3 data
row4 data
row5 data

row6数据
row7数据

row6 data
row7 data

在这种情况下,我只需要前5行,因此do\while检查旨在在到达空白单元格时停止。不会发生这种情况,因为检查不会循环(它在完成一个圆之后就停止了,就像它发现一个空白单元格一样,即使它填充了数据也是如此)。

In this case I need only the first 5 rows, so the do\while check is intended to stop once it reaches the blank cell. This doesn't happens, because the check doesn't loop (it stops after completing a circle like it finds a blank cell even if it is filled with data).

string str;
int rCnt = 11; //the data I need is after the 10th row in excel
int cCnt = 1;
int valori = 1;

Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(label4.Text, 0, false, 5, "", "", 
                                                 false, Excel.XlPlatform.xlWindows, 
                                                 "", true, false, 0, true, false, 
                                                 false);
Excel.Sheets xlsheet = xlWorkbook.Worksheets;
string sheet1 = "Sheet1";
Excel.Worksheet xlWorksheet = (Excel.Worksheet)xlsheet.get_Item(sheet1);
Excel.Range xlCell;   

do
{
    rCnt++;
    str = "A" + rCnt;
    xlCell = (Excel.Range)xlWorksheet.get_Range(str, str);
} while (xlCell.Value2 == null);
    

我尝试将 Value2 更改为文本并尝试将==设置为

I tried changing Value2 to Value or Text and trying to set == "" instead of null.

推荐答案

如果要在到达空白单元格时停止循环,请尝试..尝试更改

If you want the loop stop when reach blank cell then .. Try to change

while (xlCell.Value2 == null);

while (! IsNull(xlCell.Value2));

这篇关于如何检查单元格是否为空(Excel\VisualC#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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