How to best fetch a cell value from excel using VSTO?

I am trying to get cells from excel into csharp but not sure what's the best variable type to read it into.

If I make the variable a string and the cell value is a double I get a parse error. If I make the variable double then when the cell is a string it wont work.

Here's the code I am running:

 try
 {
        string i = Globals.Sheet1.Cells[7, 7].Value;
        double num;
        if (i == null) return;

        if (double.TryParse(i, out num)) 
        {
              .
              .
              .
        }
}
catch (Exception e)
{
       MessageBox.Show(e.ToString());
}

Answers


Make it an object, then find out the right type after you have gotten the value out of the cell.

I don't know about VSTO, but in the Excel Interop assembly, there were a Value2 and a Text property which both returned object, and could be casted through polymorphism to the correct type. Doesn't VSTO provide those?


Need Your Help

Submlime Text 2 - Windows key behavior on Mac?

windows osx sublimetext2 keymapping

I'm using a Mac for development, but 15 years of Windows experience before that means certain keystrokes are hard-wired (like Home for beginning of line, etc).