在c++ xll中从excel接收一个矩阵,修改并返回
Receive a matrix from excel in c++ xll, modifying it and returning it
我编写了一个从 excel 接收矩阵的 xll 函数,修改它并返回它:
__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
注册输入参数。
您需要正确声明 return 变量:
// initialize the return variable
xlArray.val.array.rows = rows;
xlArray.val.array.columns = cols;
xlArray.xltype = xltypeMulti | xlbitDLLFree;
我用 ujsgeyrr1f0d0d0r0h1h0j0j_juj 代码制作了一个来测试我的应用程序。我认为它可能有用。我也使用了其他参考文献中的代码。
__declspec(dllexport) LPXLOPER12 WINAPI Array_In_Out(LPXLOPER12 ExMatriz)
{
static XLOPER12 xlMatriz; // variável de retorno
try
{
int rows = ExMatriz->val.array.rows;
int cols = ExMatriz->val.array.columns;
xlMatriz.xltype = xltypeMulti | xlbitDLLFree;
xlMatriz.val.array.rows = rows;
xlMatriz.val.array.columns = cols;
// ======= VERIFICACAO DOS DADOS DE ENTRADA ==============
if (ExMatriz->xltype != xltypeRef &&
ExMatriz->xltype != xltypeSRef &&
ExMatriz->xltype != xltypeMulti)
{
xlMatriz.xltype = xltypeErr;
xlMatriz.val.err = xlerrValue;
return (LPXLOPER12)&xlMatriz;
}
static XLOPER12 MatrizMulti, tempTypeMulti;
LPXLOPER12 Matrizptr;
tempTypeMulti.xltype = xltypeInt;
tempTypeMulti.val.w = xltypeMulti;
if (xlretUncalced ==
Excel12(xlCoerce, (LPXLOPER12)&MatrizMulti, 2, (LPXLOPER12)ExMatriz,
(LPXLOPER12)&tempTypeMulti))
{
return 0;
}
Matrizptr = MatrizMulti.val.array.lparray;
// verify that the entire ExMatriz are nums
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
if ((Matrizptr + ((r* cols) + c))->xltype != xltypeNum) // verifica se os valores são numericos
{
if (Matrizptr->xltype == xltypeErr)
{
xlMatriz.xltype = xltypeErr;
xlMatriz.val.err = Matrizptr->val.err;
return (LPXLOPER12)&xlMatriz;
}
else
{
xlMatriz.xltype = xltypeErr;
xlMatriz.val.err = xlerrValue;
return (LPXLOPER12)&xlMatriz;
}
}
}
}
xlMatriz.val.array.lparray = reinterpret_cast<LPXLOPER12>(::new XLOPER12[rows * cols]); //new XLOPER12[rows*cols];
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
xlMatriz.val.array.lparray + ((r* cols) + c);
ExMatriz->val.array.lparray + ((r* cols) + c);
xlMatriz.val.num = ExMatriz->val.num; // coloca os valores de entrada na variável de saída !!!
}
}
return static_cast<LPXLOPER12>(&xlMatriz);
//return (LPXLOPER12)&xlMatriz;
}
catch (std::exception& e)
{
xlMatriz.xltype = xltypeErr;
xlMatriz.val.err = xlerrNA;
return (LPXLOPER12)&xlMatriz;
}
}
我编写了一个从 excel 接收矩阵的 xll 函数,修改它并返回它:
__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
注册输入参数。 您需要正确声明 return 变量:
// initialize the return variable xlArray.val.array.rows = rows; xlArray.val.array.columns = cols; xlArray.xltype = xltypeMulti | xlbitDLLFree;
我用 ujsgeyrr1f0d0d0r0h1h0j0j_juj 代码制作了一个来测试我的应用程序。我认为它可能有用。我也使用了其他参考文献中的代码。
__declspec(dllexport) LPXLOPER12 WINAPI Array_In_Out(LPXLOPER12 ExMatriz)
{
static XLOPER12 xlMatriz; // variável de retorno
try
{
int rows = ExMatriz->val.array.rows;
int cols = ExMatriz->val.array.columns;
xlMatriz.xltype = xltypeMulti | xlbitDLLFree;
xlMatriz.val.array.rows = rows;
xlMatriz.val.array.columns = cols;
// ======= VERIFICACAO DOS DADOS DE ENTRADA ==============
if (ExMatriz->xltype != xltypeRef &&
ExMatriz->xltype != xltypeSRef &&
ExMatriz->xltype != xltypeMulti)
{
xlMatriz.xltype = xltypeErr;
xlMatriz.val.err = xlerrValue;
return (LPXLOPER12)&xlMatriz;
}
static XLOPER12 MatrizMulti, tempTypeMulti;
LPXLOPER12 Matrizptr;
tempTypeMulti.xltype = xltypeInt;
tempTypeMulti.val.w = xltypeMulti;
if (xlretUncalced ==
Excel12(xlCoerce, (LPXLOPER12)&MatrizMulti, 2, (LPXLOPER12)ExMatriz,
(LPXLOPER12)&tempTypeMulti))
{
return 0;
}
Matrizptr = MatrizMulti.val.array.lparray;
// verify that the entire ExMatriz are nums
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
if ((Matrizptr + ((r* cols) + c))->xltype != xltypeNum) // verifica se os valores são numericos
{
if (Matrizptr->xltype == xltypeErr)
{
xlMatriz.xltype = xltypeErr;
xlMatriz.val.err = Matrizptr->val.err;
return (LPXLOPER12)&xlMatriz;
}
else
{
xlMatriz.xltype = xltypeErr;
xlMatriz.val.err = xlerrValue;
return (LPXLOPER12)&xlMatriz;
}
}
}
}
xlMatriz.val.array.lparray = reinterpret_cast<LPXLOPER12>(::new XLOPER12[rows * cols]); //new XLOPER12[rows*cols];
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
xlMatriz.val.array.lparray + ((r* cols) + c);
ExMatriz->val.array.lparray + ((r* cols) + c);
xlMatriz.val.num = ExMatriz->val.num; // coloca os valores de entrada na variável de saída !!!
}
}
return static_cast<LPXLOPER12>(&xlMatriz);
//return (LPXLOPER12)&xlMatriz;
}
catch (std::exception& e)
{
xlMatriz.xltype = xltypeErr;
xlMatriz.val.err = xlerrNA;
return (LPXLOPER12)&xlMatriz;
}
}