从c ++ xll返回多维数组到excel [英] return multi dimension array to excel from c++ xll
问题描述
当我尝试传递一维数组时,这很好用
This works fine when i try to pass a 1 dimension array
__declspec(dllexport) LPXLOPER TestArray()
{
XLOPER xlValues[2];
xlValues[0].xltype = xltypeNum;
xlValues[1].xltype = xltypeNum;
xlValues[0].val.num = 123;
xlValues[1].val.num = 345;
static XLOPER xlArray;
xlArray.xltype = xltypeMulti | xlbitDLLFree;
xlArray.val.array.rows = 2;
xlArray.val.array.columns = 1;
xlArray.val.array.lparray = &xlValues[0];
return (LPXLOPER)&xlArray;
}
但是当我尝试传递多维数组时,该函数返回#NUM!
But when i try to pass a multi dimension array, the function returns #NUM!
__declspec(dllexport) LPXLOPER TestArray1()
{
XLOPER xlValues[2][2];
xlValues[0][0].xltype = xltypeNum;
xlValues[0][1].xltype = xltypeNum;
xlValues[1][0].xltype = xltypeNum;
xlValues[1][1].xltype = xltypeNum;
xlValues[0][0].val.num = 123;
xlValues[0][1].val.num = 456;
xlValues[1][0].val.num = 345;
xlValues[1][1].val.num = 43456;
static XLOPER xlArray;
xlArray.xltype = xltypeMulti | xlbitDLLFree;
xlArray.val.array.rows = 2;
xlArray.val.array.columns = 2;
xlArray.val.array.lparray = &xlValues[0][0];
return (LPXLOPER)&xlArray;
}
有什么想法吗?预先感谢!
Any ideas? thanks in advance!!
推荐答案
在TestArray()和TestArray1()中,xlValues是堆栈上的局部变量,因此当函数返回时,运行时将释放它.您需要使xlValues堆分配的内存能够可靠地工作. XLL开发是一门黑暗的艺术.如果您对此打算认真对待,则应该投资购买史蒂夫·道尔顿(Steve Dalton)的书.
In both TestArray( ) and TestArray1( ) xlValues is a local variable on the stack, so it will be freed by the runtime when the function returns. You need to make xlValues heap allocated memory for this to work reliably. XLL development is something of a dark art. If you're going to get serious about it you should invest in a copy of Steve Dalton's book.
这篇关于从c ++ xll返回多维数组到excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!