vba 在 C# 中使用 interop.excel 检测空白 Excel 单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28435328/
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
Detect blank Excel cell using interop.excel in C#
提问by kPuck
I'm trying to parse text from an Excel doc in C#. The problem is that I can't find a way to detect (and ignore) blank cells.
我正在尝试从 C# 中的 Excel 文档中解析文本。问题是我找不到检测(和忽略)空白单元格的方法。
I've searched this forum and many others but I can still not get it to work.
我已经搜索了这个论坛和许多其他论坛,但我仍然无法让它工作。
I'm using Microsoft.Office.Interop.Excel in Visual Studio 2010 C#. I have no problem with opening Excel and reading and storing data in it. Its just that I can't skip blank cells so my program will consequentially store blank values.
我在 Visual Studio 2010 C# 中使用 Microsoft.Office.Interop.Excel。我打开 Excel 并在其中读取和存储数据没有问题。只是我不能跳过空白单元格,因此我的程序将因此存储空白值。
I have defined my woorksheet the following way:
我已经通过以下方式定义了我的工作表:
Excel.Application exApp = OpenExcel();
String mySheet = @"C:\sheet.xlsx";
Excel.Workbook wb = exApp.Workbooks.Open(mySheet);
Excel.Worksheet ws = wb.Sheets[1];
Excel.Application exApp = OpenExcel();
String mySheet = @"C:\sheet.xlsx";
Excel.Workbook wb = exApp.Workbooks.Open(mySheet);
Excel.Worksheet ws = wb.Sheets[1];
I access my cells by ws.Cells[row, col]
. And I've tried different approaches. To name a few:
我通过ws.Cells[row, col]
. 我尝试了不同的方法。仅举几例:
ws.Cells[row, col].Value2 == null
ws.Cells[row, col].Value == null
ws.Cells[row, col].Value == ""
And also using the Range object explicitely
Excel.Range rng = ws.Cells[row, col]
To use rng.Value2
并且还明确Excel.Range rng = ws.Cells[row, col]
地使用Range 对象
来使用rng.Value2
Depending on which apprach I use I get different errors, Exception from HRESULT: 0x800A03EC is one of them.
根据我使用的方法,我会收到不同的错误,来自 HRESULT 的异常:0x800A03EC 就是其中之一。
Have I missed something fundamental, surely this must be a common task?
我是否错过了一些基本的东西,这肯定是一项常见的任务吗?
If some kind soul could show me a good way to detect blank cells I would appreciate it very much!
如果有好心人能告诉我一种检测空白单元格的好方法,我将不胜感激!
Kind regards
亲切的问候
采纳答案by Philippe.H
You should probably use SpecialCells. E.g. the code below sets the value of all empty cells to zero (vba code).
您可能应该使用 SpecialCells。例如,下面的代码将所有空单元格的值设置为零(vba 代码)。
Range("e11:g16").SpecialCells(xlCellTypeBlanks).Value = 0
Or you can easily loop all empty cells like this:
或者您可以轻松地循环所有空单元格,如下所示:
For Each item In Range("e11:g16").SpecialCells(xlCellTypeBlanks)
'...
Next
In the same logic, if you want to loop through all cells which are not empty:
在相同的逻辑中,如果要遍历所有非空单元格:
For Each item In Range("g10:h11").SpecialCells(xlCellTypeConstants)
'...
Next
回答by el_guazu
Try with the Text property of the Range object...
尝试使用 Range 对象的 Text 属性...
rng.Text == String.Empty
回答by Ric Gaudet
Try this:
尝试这个:
if (ws.IsEmpty(Cells[row, col]))
I don't exactly remember if I got the syntax right for porting to Excel from .NET, but this is the command I use in a VB macro from within Excel.
我不记得我是否有正确的语法从 .NET 移植到 Excel,但这是我在 Excel 中的 VB 宏中使用的命令。
you can also try IsNumeric(Cells[row,col].Value)
你也可以试试 IsNumeric(Cells[row,col].Value)
The default value for Excel cells is Empty (but I can't find the MSN link to verify this!)
Excel 单元格的默认值为空(但我找不到 MSN 链接来验证这一点!)
See: What is the difference between =Empty and IsEmpty() in VBA (Excel)?