简体   繁体   English

从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;
}

But when i try to pass a multi dimension array, the function returns #NUM! 但是,当我尝试传递多维数组时,该函数返回#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!! 提前致谢!!

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. 在TestArray()和TestArray1()中,xlValues是堆栈上的局部变量,因此当函数返回时,运行时将释放它。 You need to make xlValues heap allocated memory for this to work reliably. 您需要使xlValues堆分配的内存能够可靠地工作。 XLL development is something of a dark art. XLL开发是一门黑暗的艺术。 If you're going to get serious about it you should invest in a copy of Steve Dalton's book. 如果您要认真对待它,则应该购买史蒂夫·道尔顿(Steve Dalton)的书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM