c++ - 如何使用带有范围的 C++ 创建 XLL 文件?
问题描述
我想使用 c++ 创建用户函数 Excel。我创建了简单的用户函数,但我不知道如何使用参数是范围来创建它。
感谢您阅读并帮助我。
解决方案
要让用户定义函数 (UDF) 接受 XL 范围作为参数或返回变量,您需要在 c++ 中使用 XLOPER。这是 Microsoft Excel SDK 的一部分。我建议您从 SDK 中编译一些示例代码,然后从那里开始了解它。
这是 SDK 中的一个示例,其中 LPXLOPER12 用于 XL 2007+
/*
** fArray
**
** This example consists of two routines: fArray and xlAutoFree().
** This function creates an xltypeMulti containing eight values. It returns
** this array to Microsoft Excel with the xlbitDLLFree bit set. When
** Microsoft Excel is done with the values, it calls xlAutoFree(), which
** frees the memory that fArray() allocated.
*/
HANDLE hArray;
__declspec(dllexport) LPXLOPER12 WINAPI fArray(void)
{
LPXLOPER12 pxArray;
static XLOPER12 xMulti;
int i;
int rwcol;
xMulti.xltype = xltypeMulti | xlbitDLLFree;
xMulti.val.array.columns = 1;
xMulti.val.array.rows = 8;
//For large values of rows and columns, this would overflow
//use __int64 in that case and return an error if rwcol
//contains a number that won't fit in sizeof(int) bytes
rwcol = xMulti.val.array.columns * xMulti.val.array.rows;
pxArray = (LPXLOPER12)GlobalLock(hArray = GlobalAlloc(GMEM_ZEROINIT, rwcol * sizeof(XLOPER12)));
xMulti.val.array.lparray = pxArray;
for(i = 0; i < rwcol; i++)
{
pxArray[i].xltype = xltypeInt;
pxArray[i].val.w = i;
}
//Word of caution - returning static XLOPERs/XLOPER12s is not thread safe
//for UDFs declared as thread safe, use alternate memory allocation mechanisms
return (LPXLOPER12)&xMulti;
}
__declspec(dllexport) void WINAPI xlAutoFree12(LPXLOPER12 pxFree)
{
GlobalUnlock(hArray);
GlobalFree(hArray);
return;
}
推荐阅读
- ios - Flutter:命令 PhaseScriptExecution 失败且没有错误
- python - 多线程按顺序迭代
- python - 如何在 Python 中将 DNA 列表序列转换为蛋白质序列
- google-chrome - 如何删除它,因为我的浏览器不断更改为 Yahoo 而不是 Google
- vue.js - 代码块中的 Vue 组件,Vue 试图渲染模板
- node.js - 从前端接收空文件对象到后端 React 和 NodeJS
- php - Laravel 没有这样的文件或目录
- graphql - 切换到 graphql 15.5.1 后返回空数组会导致“无法返回空值”错误
- r - 获取不带组的每 n 行的均值
- spring-boot - Azure 媒体服务,VideoJS,需要在 src 中隐藏 Azure 端点 url