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.
I'm using Microsoft.Office.Interop.Excel in Visual Studio 2010 C#. I can't skip blank cells so my program will consequentially store blank values.
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];
I access my cells by ws.Cells[row, col]
. And I've tried different approaches. To name a few:
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
Depending on which approach I use I get different errors, Exception from HRESULT: 0x800A03EC is one of them.
You should probably use SpecialCells. Eg the code below sets the value of all empty cells to zero (vba code).
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
尝试使用 Range 对象的 Text 属性...
rng.Text == String.Empty
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.
you can also try IsNumeric(Cells[row,col].Value)
The default value for Excel cells is Empty (but I can't find the MSN link to verify this!)
See: What is the difference between =Empty and IsEmpty() in VBA (Excel)?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.