您的位置首页百科问答

怎么使用COleSafeArray实现二维数组将字符串写入excel

怎么使用COleSafeArray实现二维数组将字符串写入excel

的有关信息介绍如下:

怎么使用COleSafeArray实现二维数组将字符串写入excel

如何使用COleSafeArray实现二维数组将字符串写入excel// VARTYPE vt = VT_BSTR ; /*数组元衫携瞎素的类型,string*/ VARTYPE vt = VT_I4; /*数组元素的类型,long*/ SAFEARRAYBOUND sabWrite[2]; /*用于定义数组的维数和下标的起始值*/ sabWrite[0].cElements = 1; sabWrite[0].lLbound = 0; sabWrite[1].cElements = 3; sabWrite[1].lLbound = 0; COleSafeArray olesaWrite; olesaWrite.Create(vt, sizeof(sabWrite)/sizeof(SAFEARRAYBOUND), sabWrite); /*通过指向数或空组的指针来对二维数组的元素进行间接赋值*/ long (*pArray)[2] = NULL; olesaWrite.AccessData((void **)&pArray); memset(pArray, 0, sabWrite[0].cElements * sabWrite[1].cElements * sizeof(long)); /*释放指向数组的指针*/ olesaWrite.UnaccessData(); pArray = NULL; /*对二维数组的元素进行逐个赋值*/ long index[2] = {0, 0}; long lFirstLBound = 0; long lFirstUBound = 0; long lSecondLBound = 0; long lSecondUBound = 0; olesaWrite.GetLBound(1, &lFirstLBound); olesaWrite.GetUBound(1, &lFirstUBound); olesaWrite.GetLBound(2, &lSecondLBound); olesaWrite.GetUBound(2, &lSecondUBound); for (long i = lFirstLBound; i <隐轿= lFirstUBound; i++) { index[0] = i ; for (long j = lSecondLBound; j <= lSecondUBound; j++) { index[1] = j; long lElement = str[j]; //CString lElement = ch; olesaWrite.PutElement(index, &lElement);} } /*把ColesaWritefeArray变量转换为VARIANT,并写入到Excel表格中*/ VARIANT varWrite = (VARIANT)olesaWrite; range.put_Value2(varWrite); 这样能将输入的整形写入excel,当变成VT_BSTR报内存不足,求指导如何修改------解决方案--------------------可以通过另外一种写法,CRange write_range = start_range.get_Offset(COleVariant((long)7),COleVariant((long)j)) ; write_range.put_Value2((COleVariant)(str[j]));