02-19-2015 09:11 AM
Hello,
I want to make an array on Excel. For that, I'm using two functions : Excel_WorksheetGetRange and Excel_RangeSetValue2.
I write the array on Excel file but only the first case of the array is copied.
Here, a part of my program :
char * DataToWrite = {"A", "B", "C", "D"}
char cCell[10];
VARIANT vCell, Data;
ExcelObj_Worksheet hWorksheet;
ExcelObj_Range hRange;
...
CA_VariantSetCString (&vCell,cCell); //cCell = A1:A4
Excel_WorksheetGetRange (hWorksheet, NULL, vCell, CA_DEFAULT_VAL, &hRange);
CA_VariantSetCString (&Data,DataToWrite );
Excel_RangeSetValue2 (hRange, NULL, Data);
Here what I obtain when I'm compiling on Excel file :
A
A
A
A
Solved! Go to Solution.
02-20-2015 02:20 AM
You should use other instructions to fill the range of cells:
strcpy (msg, "A1:A4"); CA_VariantSetCString (&MyVariant, msg); Excel_WorksheetRange (ExcelWorksheetHandle, NULL, MyVariant, CA_DEFAULT_VAL, &ExcelRangeHandle); Excel_RangeActivate (ExcelRangeHandle, NULL, NULL);
// Create a SafeArray from array of data CA_VariantSet2DArray (&MyVariant, CAVT_CSTRING, righe, 1, array); // Transfer data to Excel range Excel_SetProperty (ExcelRangeHandle, NULL, Excel_RangeValue2, CAVT_VARIANT, MyVariant);
ClearObjHandle (ExcelRangeHandle);
02-20-2015 03:38 AM
Thank you for your solution, it works. It helps me to find one with another functions :
char strCell[10];
VARIANT vCell,vData;
ExcelObj_Range hRange;
CA_VariantSetCString (&vCell, strCell);
Excel_WorksheetGetRange (hWorksheet, NULL, vCell, CA_DEFAULT_VAL, &hRange);
Excel_RangeActivate (hRange, NULL, NULL);
// Create a SafeArray from array of data
CA_VariantSet2DArray (&vData, CAVT_CSTRING, iNbElts, 1, strDataToWrite);
// Transfer data to Excel range
Excel_RangeSetValue2 (hRange, NULL, vData);
CA_DiscardObjHandle (hRange);