简体   繁体   English

从C ++ XLL将二维数组返回到Excel,返回

[英]Return two dimensional array to excel from a c++ xll, the come back

First of all I know that the question was already asked here : 首先,我知道这个问题已经在这里提出了:

return multi dimension array to excel from c++ xll 从c ++ xll返回多维数组到excel

I have tried to revive the subject, without success. 我试图重振这个主题,但没有成功。 (The OP not being that active lately, never was.) That's why I am asking the question again, sorry in advance. (OP最近没有那么活跃,从来没有。)这就是为什么我再次问这个问题,对不起。

I coded up a function returning a (row) one dimensional array : 我编写了一个返回(行)一维数组的函数:

__declspec(dllexport) LPXLOPER12 WINAPI Get1DArray(void)
{
    static XLOPER12 xlArray;
    XLOPER12 xlValues[2];
    xlValues[0].xltype = xltypeNum;
    xlValues[1].xltype = xltypeNum;
    xlValues[0].val.num = 123;
    xlValues[1].val.num = 456;
    xlArray.xltype = xltypeMulti | xlbitDLLFree;
    xlArray.val.array.rows = 1;
    xlArray.val.array.columns = 2;
    xlArray.val.array.lparray = &xlValues[0];
    return static_cast<LPXLOPER12>(&xlArray);
}

that works. 这样可行。 I tried the same wrong thing the OP from question I was mentionning above tried (that's how I came across the question of his). 我在上面提到的问题中尝试了OP的同样错误尝试(这就是我遇到他的问题的方式)。

The only doc I have is the msdn for excel sdk, it did not help me. 我唯一的文档是excel sdk的msdn,它对我没有帮助。 The function I coded, I used an example found on the web. 我编写的函数,我使用了网上的示例。 Did not find any for two-dimensional array. 找不到任何二维数组。 I know Steve Dalton's books about xll, did not help. 我知道史蒂夫·道尔顿(Steve Dalton)有关xll的书并没有帮助。

I suspect multidimensional XLOPER12 arrays stock values in one-dimensional arrays, numbering by rows and columns or columns and rows, but did not succeed in exploiting this intuition... 我怀疑多维XLOPER12数组将一维数组中的值存储起来, XLOPER12行和列或列和行进行编号,但未能成功利用这种直觉...

That's why I am here. 这就是为什么我在这里。

A simple example returning a 5*5 matrix. 一个简单的示例返回一个5 * 5矩阵。 Don't forget to free the allocated array via the xlAutoFree12 function. 不要忘记通过xlAutoFree12函数释放分配的数组。

__declspec(dllexport) LPXLOPER12 WINAPI Get2DArray(void)
{   
    static XLOPER12 xlArray;
    int rows = 5;
    int cols = 5;
    xlArray.xltype = xltypeMulti | xlbitDLLFree;
    xlArray.val.array.rows = rows;
    xlArray.val.array.columns = cols;
    xlArray.val.array.lparray = reinterpret_cast<LPXLOPER12>(::malloc(rows * cols * sizeof(XLOPER12)));
    for (int r=0;r<rows;r++)
    {
            for (int c=0;c<cols;c++)
            {
                XLOPER12* var = xlArray.val.array.lparray + ((r* cols) + c);
                var->xltype = xltypeNum;
                var->val.num = r*c;
            }
    }
    return &xlArray;
}

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

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