# Excel函数使用——求方差（Var或VarP）

int arg1[40] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40};

VARIANT varArg1;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = 40;          // 这个是数组的长度。

psa = SafeArrayCreate(VT_I4, 1, rgsabound); // VT_I4指定4byte的
signed int
for (long k = 0; k < (long)40; k++)

SafeArrayPutElement (psa, &k, &arg1[k]); // 这里是转换

varArg1.vt = VT_ARRAY ¦ VT_I4;  // 指定VARIANT的数据类型，整型数组

varArg1.parray = psa; // 赋值

// 计算方差，不用的参数必须传递vtMissing，相当于
NULL
double dbRetVal = oFun.Var( varArg1,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing,
vtMissing );
// 记住用完一定要释放SAFEARRAY, 一面内存泄露

SafeArrayDestroyData( psa );

136.66666

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

11-19

10-29 508
08-19 1136
12-16 4096
03-23 238
06-24 1万+
03-06 780
09-11 272