03-24-2011 09:56 AM - edited 03-24-2011 10:02 AM
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!
03-27-2011 01:29 AM
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
03-27-2011 02:34 AM
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.
04-01-2011 06:46 AM
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!
04-01-2011 06:49 AM - edited 04-01-2011 06:50 AM
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.