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

I have a VSTO add-on and am reading data from an Excel worksheet.

It seems that almost all numerical data is read as double. Is it possible to get the value intfrom 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 what format I use on Sheet Sheet1, cell A1 isIntalways returns false.

Is there any format I have to use to get int? I thought, maybe, Generalor 0will work, but it is not.

+5
source share
4 answers

docs var value = ((Range)w.Cells[1,1]).Value Value . , , Excel , doubles, int s. "" Excel, #, integer . Value , Excel.

, Excel .

0

, Range.Value, (, Excel, , , )

int, GetValue, ErrorCodes

.

  • DIV/0! Int32 -2146826281
+2

, , Excel .

Excel stores numerical values ​​as Double Precision Floating Pointnumbers, or, as you know, Doublesfor short.

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

EDIT

Since you are looking for a link, see this

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

+1
source

I am sure all numbers are doubled in Excel. For example, the following VBA code sets a cell value to a long integer (Int32) and then reads it back.

Dim n As Long
n = 123
Range("A1").Value = n
Debug.Print VarType(n)

Dim v As Variant
v = Range("A1").Value
Debug.Print VarType(v)
  • Input - VarType (n) = 3 = vbLong (Int32)
  • Output - VarType (v) = 5 = vbDouble
+1
source

All Articles