return multi dimension array to excel from c++ xll

会有一股神秘感。 提交于 2019-12-13 04:54:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!