IDE: Embarcadero XE5
I am attempting to improve the performance(speed) of an 'export to excel' procedure. The procedure contains way too many OLE function calls and property read/write calls, hence the poor performance.
At present, a grid(2D array) is exported to excel by stepping through each cell in the grid and setting it's value.
I'm trying to export the entire grid into a excel cell-range at once, but failing in my attempts.
Now for Embarcadero Delphi-users this seems to be a trivial task:
// NOTE: This obviously won't compile, just showing the process.
var
arrData: Variant;
begin
// Create a 2D Variant array
arrData := VarArrayCreate([1, RowCount, 1, ColCount], varVariant);
// Fill array with values...
// Get a range of cells, size equal to arrData dimensions...
Range.Value := arrData; // Done, easy.
end;
For Embarcadero c++ -users however(or perhaps just myself), it doesn't seem to be all that obvious.
I've managed to create and fill a single-dimensional array with data, using: VarArrayCreate, Embarcadero Example.
I tried to assign that array to a cell-range as follows:
Variant vArray;
// Create and fill vArray with data. Test case.
int bounds[2] = {0, 4};
// Creates Variant array containing 5 elements of type varVariant.
// Can set the element type to varInteger as well for this case, made no difference to the end result.
vArray = VarArrayCreate( bounds, 1, varVariant );
// Fill vArray with test data, values 0,1,2,3,4
for ( int index = 0; index < 5; index++ ) {
VarArrayPut( vArray, index, &index, 0 );
}
Variant vWorkSheet; // Active excel worksheet
Variant vCells = vWorkSheet.OlePropertyGet( L"Cells" );
Variant vRange; // A excel cell-range, equal in size to my vArray's size.
Variant vRange = vCells.OlePropertyGet( L"Range", vCells.OlePropertyGet(L"Item", 1, 1), vCells.OlePropertyGet(L"Item", 5, 1) );
vRange.OlePropertySet( L"Value", vArray ); // Similar to what is done in Delphi in example above.
...resulting in the entire excel cell-range being filled with the first value in vArray
.
How would you go about creating multi-dimensional Variant arrays(two-dimensional in this case)?
How do you assign the Variant array to an Excel cell-range using OLE?