LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

iproblem with reading date(various of type ) from excel . please help!

i try the example excel2000dem ,and it run well. but when i open another excel ,because i want to read other types of date,  it does not work well.

here is my code:

 

// i want to read int from the excel

 

 // Get the value of the Single Cell Range

error = Excel_GetProperty (ExcelSingleCellRangeHandle, &ErrorInfo, Excel_RangeValue2, CAVT_VARIANT, &MyVariant);  // i can see MyVariant ={R8, 2245 }

 

error = CA_VariantGetInt(&MyVariant, &d);   // this line does not work,   and get an error message''

 

 //it seem when i use  CA_VariantGetDouble(&MyVariant, &df );   it  work well, df = 2245.000000, but i want get the int 2245, not double .

 

.........  it seems i didnt understand the MyVariant well, the struct.

 

and  if i want to get string or char, how to ??? thank you!

 

0 Kudos
Message 1 of 5
(3,346 Views)

Hi , 

 

try using a cell with no presition points.

 

but if it doesn't work then just get the nubmer as a double and cast it in to an int.

 

double numA = 5.5;

int numB = 0;

 

numB = numA;

 

this will give you that numB = 5

 

-----------------------------------------
Kobi Kalif
Software Engineer

0 Kudos
Message 2 of 5
(3,334 Views)

The problem here is that when you read data from an Excel cell you are actually retrieving the content of the cell together withits data type: you cannot get a string from a variant that is storing a number! Even if the variant is capable of storing various data types in the same variable, it does not mean that it stores all of them in the same moment: the type of data stored in the variant is assigned either when passing data to variant via CA_VariantSetXxx functions or in your case when reading from Excel file. Trying to retrieve an incorrect data type from the variant results in an error, which you are actually getting (it should be error -2147352571 meaning incompatible data types).

 

ActiveX library offers you the command CA_VariantGetType which returns the data type actally associated with the given variant; there are also several CA_VariantHasXxx functions to find if a variant is storing a specific data type.

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 3 of 5
(3,332 Views)

To Roberto Bozzolo :

 

 I have alreay used the command CA_VariantGetType and CA_VariantHasXxx functions ,but when the cell's value is an integer  such as 6515, 

I used the command CA_VariantGetType and  CA_VariantHasInt , it shows that it didn't has Integer,

then  i try   the command CA_VariantHasDouble,It has double. 

So,what should i do? i really don't know.

thank you!

 

0 Kudos
Message 4 of 5
(3,315 Views)

TO Kobi Kalif :

yes, before i find a method to solve the problem, i am doing so . 

double  A;

int        B;

 

B = ( int ) A;  

But I really want to know how to do this in a better way.

 

0 Kudos
Message 5 of 5
(3,314 Views)