Reading Excel data using getvalue in C++

  • Thread starter Thread starter suryahatmaja
  • Start date Start date
S

suryahatmaja

Guest
I have <g class="gr_ gr_21 gr-alert gr_gramm gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="21" id="21">problem</g> in reading data from Excel using C++. Initially, I use getItem() function to read. Unfortunately, it takes <g class="gr_ gr_22 gr-alert gr_gramm gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="22" id="22">time</g> to read some big range size. I know that we can write using getItem() function and also it will take time.

Fortunately, we can use <g class="gr_ gr_26 gr-alert gr_gramm gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="26" id="26">value2</g> function in writing that combined with SafeArrayGetElement.

I think we can use the opposite of code (to read instead of to write) and use SafeArrayGetElement, but I couldn't get the data from excel using it.

this is my code to read the data from Excel using value2:



HRESULT hr;_variant_t tcvt = rngSh->GetValue2();
if (tcvt.vt == (VT_ARRAY | VT_VARIANT))

{

SAFEARRAY * sa = tcvt.parray; long colMax; hr = ::SafeArrayGetUBound(sa, 1, &colMax); long colMin; hr = ::SafeArrayGetLBound(sa, 1, &colMin); cout << "colMax = " << colMax << endl; cout << "colMin = " << colMin << endl;
long index[2];

index[1] = 1;

for (long colIdx = colMin; colIdx <= colMax; colIdx++)

{

index[0] = colIdx;

VARIANT thisCell;

//thisCell.vt = VT_I8;

//thisCell.lVal = 8;

VariantInit(&thisCell);

hr = SafeArrayGetElement(sa, index, (void*)&thisCell);

if (FAILED(hr))

{

cout << "NOT OK " << endl;

}

else

{

cout << "cell value = " << thisCell.lVal << endl;

}

}

}




Anyone have <g class="gr_ gr_44 gr-alert gr_gramm gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="44" id="44">solution</g>, please welcome.

thanks...

Continue reading...
 
Back
Top