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());
}
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
Value2and a Text property which both returned object, and could be casted through polymorphism to the correct type. Doesn’t VSTO provide those?