View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Jeff Jeff is offline
external usenet poster
 
Posts: 921
Default Unable to read cell value when cell contains a string using CSharp

I am using C# to read values from an Excel file using the following code.

object ovalue = ((Microsoft.Office.Interop.Excel.Range)myWorksheet .Cells[1,
1]).Value2;


string value = ovalue == null ? "" : ovalue.ToString();


If the target cell contains an integer, I can retrieve the value without
issue. If the cell contains a string, ovalue is null so the string cannot be
read.

I have experimented with deleting data from other cells in the spreadsheet
and sometimes I am able to delete random cells which will then allow the
strings to read properly. This suggests some kind of corruption causing the
value to get returned as null. There is no indication of a problem in the
Visual Studio debugger output.

In one case, I recreated the entire spreadsheet and copied all the cells
from the corrupted spreadsheet to the new one. This seemd to fix the problem.

I run into this fairly frequently and would like any thoughts on what might
be happening and how to avoid the issue.