Unable to read cell value when cell contains a string using CSharp

J

Jeff

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top