从C ++ xll中的excel接收矩阵,对其进行修改并返回 [英] Receive a matrix from excel in c++ xll, modifying it and returning it

查看:113
本文介绍了从C ++ xll中的excel接收矩阵,对其进行修改并返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个xll函数,用于接收来自excel的矩阵,然后对其进行修改并返回:

__declspec(dllexport) LPXLOPER12 WINAPI ZZZZUpdateArray1(LPXLOPER12 arrayin)
{
    if (arrayin->xltype == xltypeMulti | xlbitDLLFree)
    {
        double ScaleFactor = 2.0;
        int rows = arrayin->val.array.rows;
        int cols = arrayin->val.array.columns;
        static XLOPER12 xlArray;
        xlArray.val.array.lparray = reinterpret_cast<LPXLOPER12>(::new XLOPER12[rows * cols] /*::malloc(rows * cols * sizeof(XLOPER12))*/);
        for (int r = 0; r<rows; r++)
        {
            for (int c = 0; c<cols; c++)
            {
                if ((arrayin->val.array.lparray + ((r* cols) + c))->xltype == xltypeNum)
                {
                    XLOPER12* var = xlArray.val.array.lparray + ((r* cols) + c);
                    var->xltype = xltypeNum;
                    var->val.num = ScaleFactor*(arrayin->val.array.lparray + ((r* cols) + c))->val.num ;
                }
            }
        }
        return static_cast<LPXLOPER12>(&xlArray);
    }
    return arrayin;
}

但是它崩溃了

if ((arrayin->val.array.lparray + ((r* cols) + c))->xltype == xltypeNum)

例如,如果我从excel中提取5 * 5矩阵,则在调试时我看到它有19行20列,发生了什么事?

可能是因为当我收到LPXLOPER12 arrayin时,它表示一个excel范围,比5 * 5矩阵复杂得多.如何访问它包含的5 * 5矩阵,然后呢?

解决方案

  • 使用Excel代码Q注册输入参数.
  • 您需要正确声明返回变量:

    // initialize the return variable
    xlArray.val.array.rows = rows;
    xlArray.val.array.columns = cols;
    xlArray.xltype = xltypeMulti | xlbitDLLFree;
    

I coded up an xll function receiving a matrix from excel, modifying it a returning it :

__declspec(dllexport) LPXLOPER12 WINAPI ZZZZUpdateArray1(LPXLOPER12 arrayin)
{
    if (arrayin->xltype == xltypeMulti | xlbitDLLFree)
    {
        double ScaleFactor = 2.0;
        int rows = arrayin->val.array.rows;
        int cols = arrayin->val.array.columns;
        static XLOPER12 xlArray;
        xlArray.val.array.lparray = reinterpret_cast<LPXLOPER12>(::new XLOPER12[rows * cols] /*::malloc(rows * cols * sizeof(XLOPER12))*/);
        for (int r = 0; r<rows; r++)
        {
            for (int c = 0; c<cols; c++)
            {
                if ((arrayin->val.array.lparray + ((r* cols) + c))->xltype == xltypeNum)
                {
                    XLOPER12* var = xlArray.val.array.lparray + ((r* cols) + c);
                    var->xltype = xltypeNum;
                    var->val.num = ScaleFactor*(arrayin->val.array.lparray + ((r* cols) + c))->val.num ;
                }
            }
        }
        return static_cast<LPXLOPER12>(&xlArray);
    }
    return arrayin;
}

but it crashes on

if ((arrayin->val.array.lparray + ((r* cols) + c))->xltype == xltypeNum)

If I am for instance taking a 5*5 matrix from excel, at debug I see it having 19 rows and 20 colums, what happened !?

Is it maybe because when I receive the LPXLOPER12 arrayin, it represents an excel range, way more complicated than a 5*5 matrix. How to access the 5*5 matrix it contains, then ?

解决方案

  • Use the Excel code Q to register the input argument.
  • You need to declare properly the return variable :

    // initialize the return variable
    xlArray.val.array.rows = rows;
    xlArray.val.array.columns = cols;
    xlArray.xltype = xltypeMulti | xlbitDLLFree;
    

这篇关于从C ++ xll中的excel接收矩阵,对其进行修改并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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