sending an array to Excel
First, I don't use C++ or OLE Automation, but in excel's VBA, it's usually
quicker to write the values to a range in as few calls to the worksheet as possible.
This may not help, but...
Option Explicit
Sub testme()
Dim myArr() As Long
Dim rCtr As Long
Dim cCtr As Long
Dim myCell As Range
Dim iRow As Long
Dim iCol As Long
Dim StartTime As Double
Dim EndTime As Double
ReDim myArr(1 To 1000, 1 To 100)
For rCtr = LBound(myArr, 1) To UBound(myArr, 1)
For cCtr = LBound(myArr, 2) To UBound(myArr, 2)
myArr(rCtr, cCtr) = (rCtr * 1000) + cCtr
Next cCtr
Next rCtr
Set myCell = ActiveSheet.Range("a1")
ActiveSheet.Cells.Clear
StartTime = Now
myCell.Resize(UBound(myArr, 1) - LBound(myArr, 1) + 1, _
UBound(myArr, 2) - LBound(myArr, 2) + 1).Value = myArr
EndTime = Now
Debug.Print " One write: " _
& Format(EndTime - StartTime, "hh:mm:ss.000")
ActiveSheet.Cells.Clear
StartTime = Now
iRow = -1
For rCtr = LBound(myArr, 1) To UBound(myArr, 1)
iRow = iRow + 1
iCol = -1
For cCtr = LBound(myArr, 2) To UBound(myArr, 2)
iCol = iCol + 1
myCell.Offset(iRow, iCol).Value = myArr(rCtr, cCtr)
Next cCtr
Next rCtr
EndTime = Now
Debug.Print "Multiple writes: " _
& Format(EndTime - StartTime, "hh:mm:ss.000")
End Sub
On 09/02/2010 15:03, Lynn McGuire wrote:[i]
Does anyone have experience with sending an array of information to
Excel using OLE Automation and C++ ? I am looking for a way to speed
up the sending of data from our app to Excel. Currently, we send
data to Excel a cell at a time using the following code:
void PutDoubleInTableHorizontal (int row, int column, int count, const double
num [])
{
// put the doubles in horizontally
for (int i = 0; i < count; i++)
{
if (uninitializedValue == num [i])
continue;
std::string cellAddress = GetCellEquivalent (row, column + i);
VARIANT result1;
VariantInit ( & result1);
VARIANT range;
VariantInit ( & range);
range.vt = VT_BSTR;
_bstr_t address = _bstr_t (cellAddress.c_str ());
range.bstrVal = address;
OLEMethod (DISPATCH_PROPERTYGET, & result1, pExcelSheet, L"Range", 1, range);
if (result1.vt == VT_DISPATCH)
{
VARIANT result2;
VariantInit ( & result2);
VARIANT value;
VariantInit ( & value);
value.vt = VT_R8;
value.dblVal = num ;
OLEMethod (DISPATCH_PROPERTYPUT, & result2, result1.pdispVal, L"Value", 1, value);
int res = result2.vt;
result1.pdispVal - Release ();
}
}
}
I am wondering if sending an array of cells to Excel would be faster ?
Sincerely,
Lynn McGuire
--
Dave Peterson
|