在C ++中写单元格 - 没有写入值,单元格为空 [英] Writing cell in Excel from C++ - no value written, cell is blank

查看:198
本文介绍了在C ++中写单元格 - 没有写入值,单元格为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I write a cell from C++ with OLE to a cell in Excel, I get an empty cell. Whatever value is there gets overwritten to be blank. It writes in the correct cell though, so it seems the range is correct. This is my code.

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);


推荐答案

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

I did not understand how to correctly pass an argument to Excel. It needs to be a variant, not a naked 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);

这篇关于在C ++中写单元格 - 没有写入值,单元格为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆