Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get an int from Range.Value in VSTO?

Tags:

c#

int

excel

vsto

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.

like image 382
Kris Harper Avatar asked Oct 07 '22 06:10

Kris Harper


2 Answers

Numeric values returned by Range.Value are doubles (possibly becuase they are all stored as doubles in Excel, a number looking like an integer or not is the result of formatting)

The only ints that I have seen coming through GetValue are the ErrorCodes for errors

e.g.

  • DIV/0! is an Int32 with a value of -2146826281
like image 125
AlanT Avatar answered Oct 22 '22 17:10

AlanT


Govert is correct in saying that Excel never stores numbers as an integers.

Excel stores numeric values as Double Precision Floating Point numbers, or as you know Doubles for short.

Doubles are 8-byte variables that can store numbers accurate to approximately 15 decimal places.

EDIT:

Since you are looking for a link, see this

Link: http://support.microsoft.com/kb/78113

like image 38
Siddharth Rout Avatar answered Oct 22 '22 17:10

Siddharth Rout