从C ++写Excel中的单元格 - 没有写入值,单元格为空

时间:2015-10-15 13:16:43

标签: c++ excel com ole variant

当我将带有OLE的C ++单元格写入Excel中的单元格时,我得到一个空单元格。无论什么价值都被覆盖为空白。它写入正确的单元格,所以看起来范围是正确的。这是我的代码。

VARIANT arr;
BSTR val = SysAllocString(L"hello excel world");
_bstr_t(val, false);
arr.vt = VT_ARRAY | VT_VARIANT;

SAFEARRAYBOUND sab[1];
sab[0].lLbound = 1; sab[0].cElements = 1;
arr.parray = SafeArrayCreate(VT_VARIANT, 1, sab);
long indices[] = {1, 1};
SafeArrayPutElement(arr.parray, indices, (void*)&val);

AutoWrap(DISPATCH_PROPERTYPUT, NULL, range, L"Value", 1, arr);

1 个答案:

答案 0 :(得分:0)

我不明白如何正确地将参数传递给Excel。它需要是变体,而不是裸BSTR:

VARIANT arr;
BSTR val = SysAllocString(L"hello excel world");
_bstr_t(val, false);
arr.vt = VT_ARRAY | VT_VARIANT;

SAFEARRAYBOUND sab[2];
sab[0].lLbound = 1; sab[0].cElements = 1;
sab[1].lLbound = 1; sab[1].cElements = 1;
arr.parray = SafeArrayCreate(VT_VARIANT, 2, sab);
long indices[] = {1, 1};
VARIANT valvariant;
valvariant.vt = VT_BSTR;
valvariant.bstrVal = val;

SafeArrayPutElement(arr.parray, indices, (void*)&valvariant);

AutoWrap(DISPATCH_PROPERTYPUT, NULL, range, L"Value", 1, arr);