title: c++ Excel OLE automation. Setting the values of an entire cell-range 'at once'
body: <p>IDE: Embarcadero XE5</p>
  
  <p>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.</p>
  
  <p>At present, a grid(2D array) is exported to excel by stepping through each cell in the grid and setting it's value.</p>
  
  <p>I'm trying to export the entire grid into a excel cell-range at once, but failing in my attempts.</p>
  
  <p>Now for Embarcadero Delphi-users this seems to be a trivial task:</p>
  
  <pre><code>// 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;
  </code></pre>
  
  <p>For Embarcadero c++ -users however(or perhaps just myself), it doesn't seem to be all that obvious.</p>
  
  <p>I've managed to create and fill a single-dimensional array with data, using:
  <a href="http://docwiki.embarcadero.com/Libraries/XE2/en/System.Variants.VarArrayCreate%20" rel="nofollow">VarArrayCreate</a>, <a href="http://docwiki.embarcadero.com/CodeExamples/XE2/en/VariantArrays_%28C++%29" rel="nofollow">Embarcadero Example</a>.</p>
  
  <p>I tried to assign that array to a cell-range as follows:</p>
  
  <pre><code>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 &lt; 5; index++ ) {
     VarArrayPut( vArray, index, &amp;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.
  </code></pre>
  
  <p>...resulting in the entire excel cell-range being filled with the first value in <code>vArray</code>.</p>
  
  <p><strong>How would you go about creating multi-dimensional Variant arrays(two-dimensional in this case)?</strong></p>
  
  <p><strong>How do you assign the Variant array to an Excel cell-range using OLE?</strong></p>