返回二维数组,从一个c ++ xll excel中回来 [英] Return two dimensional array to excel from a c++ xll, the come back

查看:153
本文介绍了返回二维数组,从一个c ++ xll excel中回来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我知道问题已经在这里提出:

First of all I know that the question was already asked here :

返回多维数组,以从c ++ xll中获取excel

return multi dimension array to excel from c++ xll

我试图恢复主题,没有成功。 (OP最近没有活动,从来没有。)这就是为什么我再次提出问题,提前抱歉。

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.

我编写了一个返回一个(行)的函数)一维数组:

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

可以使用。我尝试了同样的错误的事情,从上面提到的问题的OP(这是我遇到他的问题)。

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).

我唯一的文档是msdn对于excel sdk,它没有帮助我。我编码的功能,我使用了一个在网页上找到的例子。没有找到任何二维数组。我知道史蒂夫·道尔顿关于xll的书,没有帮助。

The only doc I have is the msdn for excel sdk, it did not help me. 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.

我怀疑多维的 XLOPER12 维度数组,按行和列或列和行编号,但没有成功地利用这种直觉...

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...

这就是为什么我在这里。

That's why I am here.

推荐答案

返回一个5 * 5矩阵的简单示例。不要忘记通过 xlAutoFree12 函数释放分配的数组。

A simple example returning a 5*5 matrix. Don't forget to free the allocated array via the xlAutoFree12 function.

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

这篇关于返回二维数组,从一个c ++ xll excel中回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆