I have a VSTO addin and I’m reading data from an Excel worksheet.
It seems that almost all numeric data is read as a double. Is it ever possible to get an int value from Range.Value?
Here is some code to demonstrate what I mean.
Worksheet w = (Worksheet)Globals.ThisAddIn.Application.ActiveWorkbook.Worksheets["Sheet1"];
var value = ((Range)w.Cells[1, 1]).Value;
bool isInt = value is int;
bool isDouble = value is double;
No matter which format I use in worksheet Sheet1, cell A1, isInt always comes back false.
Is there some format I should be using to get int? I thought maybe General or 0 would work, but it seems not.
From the docs when you call
var value = ((Range)w.Cells[1,1]).ValuetheValuetype is derived from the value type from the Worksheet. So it would seem that your Excel worksheet has the values formatted asdoubles, notints. There is no “integer” formatting for Excel that will translate to C#, the best bet would be to cast the value as anintegerwhen you read it in. TheValuetype comes in whatever format it was within the Excel worksheet.This article shows how Excel deals with double precision floating point values.