在 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
注册输入参数。 -
你需要正确声明返回变量:
// 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;
}
}
相关文章:
- 为什么我可以通过引用修改常量返回
- 为什么在我的函数类型后使用引用运算符 (&) 允许我修改它返回的值?
- 如果返回 -1,时间() 的参数是否被修改?
- 有没有办法修改'operator->',以便'z->im'返回复数的虚部
- 如何修改此函数,以便如果函数的参数是特定结构,则返回具有较大整数的结构?
- 我可以制作一个对象方法,如果单独调用,它将自行修改,但如果在复制初始化期间调用,则会返回一个新对象?
- D3DXPLANENERAMALAGE仅返回归一化平面或对其进行修改
- 为什么当返回类型为 const 时可以修改返回值
- 如何在另一个成员函数中修改具有常量返回类型的成员函数的返回值
- 在 c++ xll 中从 excel 接收矩阵,修改并返回它
- C++为什么可以修改返回的常量引用
- 如何返回通过memcpy函数修改的两个阵列
- Clang修改析构函数中的返回值
- 数组在函数中作为参数传递并被访问,那么为什么从函数返回后数组的值会被修改呢?
- Linux x64堆栈在信号处理程序内部展开,以修改返回地址
- 有效地返回未修改的参数
- 为什么方法应该返回新实例,而不是修改实例本身
- 为什么析构函数不修改返回的变量
- 拦截 WinAPI 调用并修改返回值
- 当在模板函数内部调用时,cudaMalloc不会修改返回指针