【发布时间】:2020-08-13 05:53:27
【问题描述】:
我正在尝试从 C# 中的 Excel 文档中解析文本。问题是我找不到检测(并忽略)空白单元格的方法。
我在 Visual Studio 2010 C# 中使用 Microsoft.Office.Interop.Excel。我无法跳过空白单元格,因此我的程序将存储空白值。
我已经通过以下方式定义了我的工作表:
Excel.Application exApp = OpenExcel(); String mySheet = @"C:\sheet.xlsx"; Excel.Workbook wb = exApp.Workbooks.Open(mySheet); Excel.Worksheet ws = wb.Sheets[1];
我通过ws.Cells[row, col] 访问我的单元格。而且我尝试了不同的方法。仅举几例:
ws.Cells[row, col].Value2 == null
ws.Cells[row, col].Value == null
ws.Cells[row, col].Value == ""
并且还明确地使用 Range 对象
Excel.Range rng = ws.Cells[row, col]
使用rng.Value2
根据我使用的方法,我会收到不同的错误,HRESULT 的异常:0x800A03EC 就是其中之一。
【问题讨论】:
-
@user1274820 您无法将
null转换为字符串。 -
stackoverflow.com/a/28452164/4551007 请参阅此以获得解决方案!