如何检查单元格是否为空 (Excel\VisualC#)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17123562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a cell is empty (Excel\VisualC#)
提问by Daniele
this might be an easy question for you and I saw a lot of topics about it, but none of them gave me the answer I need.
My aim is to check line per line in the Sheet1in order to discover how many rows are, so i put a do\while that should stop once it reaches a blank cell
这对你来说可能是一个简单的问题,我看到了很多关于它的话题,但没有一个给我需要的答案。我的目标是检查每行的行Sheet1以发现有多少行,所以我放了一个 do\while,一旦它到达一个空白单元格就应该停止
Example:
例子:
row1 data
row2 data
row3 data
row4 data
row5 datarow6 data
row7 data
row1 数据
row2 数据
row3 数据
row4 数据
row5 数据row6 数据
row7 数据
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).
在这种情况下,我只需要前 5 行,因此 do\while 检查旨在在到达空白单元格后停止。这不会发生,因为检查不会循环(它在完成一个圆圈后停止,就像它找到一个空白单元格,即使它填充了数据)。
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);
I tried changing Value2to Valueor Textand trying to set == "" instead of null.
我尝试更改Value2为ValueorText并尝试设置 == "" 而不是 null。
回答by matzone
If you want the loop stop when reach blank cell then .. Try to change
如果您希望在到达空白单元格时循环停止,则 .. 尝试更改
while (xlCell.Value2 == null);
with
和
while (! IsNull(xlCell.Value2));
回答by Grant Bagwell
The issue mainly comes from when you don't know what sort of data to expect. When I work with Excel reads I often do something similar to:
这个问题主要来自于您不知道期望什么样的数据。当我使用 Excel 读取时,我经常做类似的事情:
var _cell = range.Cells[1, 2].Value2;
if (_cell.GetType() != typeof(Double))
In your instance if you always are getting a string returned then you should be able to assume the cast:
在您的实例中,如果您总是返回一个字符串,那么您应该能够假设演员阵容:
string _str = (string)(range.Cells[str, str] as Excel.Range).Value2;
and then check that is not empty.
然后检查它不为空。
回答by thedrs
Simple way to check a cell is empty:
检查单元格为空的简单方法:
if (sheet.Cells[4,3] == null || sheet.Cells[4,3].Value2 == null || sheet.Cells[4,3].Value2.ToString() == "")
MessageBox.Show(“cell on row 4 col 3 is empty”);
回答by Joshua Ferdinand
do
{
rCnt++;
str = Sheet.Cells[rCnt, 5].Value;
} while (str != null);
回答by Jarus Rev
This is working for me:
这对我有用:
using Excel = Microsoft.Office.Interop.Excel;to read excelsheet:
通过Excel = Microsoft.Office.Interop.Excel;阅读excelsheet:
var excelApp = new Excel.Application();
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(path, 0, false, 5, "", "", false, XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelWorkbook.Sheets[2];
Excel.Range excelRange = excelWorksheet.UsedRange;
int rowCount = excelRange.Rows.Count;
int colCount = excelRange.Columns.Count;
string wwdEmpty = Convert.ToString(excelRange.Cells[5, 14].value2);
// this is working code with NULL Excell cell
回答by Alatey
You can use the "Text" property of the selected cell. It can be converted to a string type via "as". The result string can already be checked as usual in C#, like not-empty
您可以使用所选单元格的“文本”属性。它可以通过“as”转换为字符串类型。结果字符串已经可以像往常一样在 C# 中检查,比如非空
string TempText = excelWorksheet.Cells[1, 1].Text as string;
if (!string.IsNullOrWhiteSpace(TempText)){
// actions
}

