问题
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;
}
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!!
回答1:
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.
来源:https://stackoverflow.com/questions/30533839/return-multi-dimension-array-to-excel-from-c-xll