ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 697|回复: 0

[原创] Delphi 开发Excel Xll

[复制链接]

TA的精华主题

TA的得分主题

发表于 2022-7-9 16:40 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
本帖最后由 774115495 于 2022-7-9 16:47 编辑

网上Delphi 开发Excel  Xll  资料很少,本代码也是从网上获取而得,删除了一些不必要的代码,优化了相关代码,可以支持Excel2000-2021 32位版本,

64位源码不免费提供,有兴趣的可以叫我QQ。
本源码使用Excel4V函数,仅支持30的参数。
Excel12V函数支持255个参数。
源代码可以详见附件下载。
以下部分代码显示
  1. {$A+,B-,C+,D+,E-,F-,G+,H-,I+,J+,K-,L+,M-,N+,O-
  2. ,P+,Q+,R-,S-,T-,U-,V+,W+,X+,Y-,Z1}

  3. library MyExcelXll_For_Win32;

  4. uses
  5.   SysUtils,
  6.   windows;

  7. // XLREF structure
  8. type
  9.   xlref = packed record
  10.     rwFirst: smallint;
  11.     rwLast: smallint;
  12.     colFirst: byte;
  13.     colLast: byte;
  14.   end;

  15.   // Returns a range of selection
  16.   XlRangeRecord = packed record
  17.     Count: word;                             // should always be $1
  18.     Sref: xlref;
  19.   end;

  20.   xlmref = packed record
  21.     count: word;
  22.     RefTbl: array[0..1000] of XlRef;
  23.   end;

  24.   lpxloper = ^XLOPER;

  25.   lpxloperArray = ^XLArray;

  26.   lpxlmref = ^xlmref;
  27.   DLLversion: shortstring = 'ExcelLib v1.00';
  28.   AddInCategory = 'ExcelLib_32';

  29. const
  30.   zlpxloper = lpxloper(nil);

  31. type
  32.   retarray = array[0..1000] of xloper;

  33.   pretarray = ^retarray;

  34. var                                          // Global data
  35.   res: xloper;
  36.   EResult: Integer;
  37.   sink: integer;
  38.   UDF_Id: Integer;
  39.   LastErrorStr: ShortString;
  40.   FuncName: string[64];
  41.   pxModuleText, pxProcedure, pxTypetext, pxFunctiontext, pxArgumentText, pxMacroType, pxCategory, pxShortcutText: txloper;
  42.   HaveRegistered: boolean;

  43. procedure setval(var v: xloper; numval: double);
  44. begin
  45.   fillchar(v, sizeof(v), 0);
  46.   v.xltype := 1;
  47.   v.val.num := numval;
  48. end;

  49. procedure SetFunctionName(S: ShortString);
  50. begin
  51.   FuncName := S;
  52. end;

  53. procedure Error(S: ShortString);
  54. begin
  55.   if LastErrorStr <> S then
  56.     LastErrorStr := FuncName + ':' + S;
  57. end;

  58. function GetSheetName: ShortString;
  59. var
  60.   xres, xsheetname: xloper;
  61.   ResStr: ShortString;
  62. begin
  63.   ResStr := '';
  64.   Eresult := Excel4V(xlfCaller, @xres, 0, [nil]);
  65.   asm
  66.         pop     sink;            // sink: integer;
  67.   end;                                       // Never Remove
  68.   if Eresult = 16 then
  69.     ResStr := 'No Caller ID'
  70.   else
  71.   begin
  72.     eresult := Excel4V(xlsheetnm, @xsheetname, 1, [@xres]);
  73.     asm
  74.         pop     sink;
  75.     end;                                     // Never Remove
  76.     if eresult = 0 then
  77.     begin
  78.       ResStr := xsheetname.val.str^;
  79.     end
  80.   end;
  81.   Eresult := Excel4V(xlfree, nil, 1, [@xres]);
  82.   asm
  83.         pop     sink;
  84.   end;                                       // Never Remove
  85.   Eresult := Excel4V(xlfree, nil, 1, [@xsheetname]);
  86.   asm
  87.         pop     sink;
  88.   end;                                       // Never Remove
  89.   Result := ResStr;
  90. end;

  91. // Returns full path & name of DLL

  92. function GetName: ShortString;
  93. begin
  94.   EResult := Excel4V(xlGetName, @res, 1, [nil]);
  95.   asm
  96.         pop     sink;
  97.   end;                                       // Never Remove
  98.   Result := res.val.str^;
  99.   EResult := Excel4V(xlfree, nil, 1, [@res]);
  100.   asm
  101.         pop     sink;
  102.   end;                                       // Never Remove
  103. end;

  104. //Generates one value of the standard Gaussian density function }

  105. function UDF_Add(DoubleX, DoubleY: Double): Double; stdcall;
  106. begin
  107.   Result := DoubleX + DoubleY;
  108. end;

  109. function Register_UDF: integer;
  110. var
  111.   s: Shortstring;
  112. begin
  113.   Res.xltype := xltypeerr;
  114.   Res.val.err := xlerrvalue;
  115.   s := GetName;
  116.   pxModuleText.SetStr(s);
  117.   pxProcedure.SetStr('UDF_Add');
  118.   pxTypeText.SetStr('BBB');                 // Double, Double
  119.   pxFunctionText.setStr('UDF_Add');
  120.   pxArgumentText.SetStr('DoubleX, DoubleY');
  121.   pxMacrotype.SetNum(1);
  122.   pxCategory.SetStr(AddInCategory);

  123.   EResult := Excel4V(xlfregister, @res, 8, [pxModuletext.thelpxloper, pxProcedure.thelpxloper, pxTypeText.thelpxloper, pxFunctionText.thelpxloper, pxArgumentText.thelpxloper, pxMacroType.thelpxloper, pxCategory.thelpxloper, zlpxloper]);
  124.   asm
  125.         pop     sink;
  126.   end;                                       // Never Remove
  127.   Result := trunc(res.val.num);
  128. end;

  129. procedure Register_All;
  130. begin
  131.   if HaveRegistered then
  132.     exit;
  133.   HaveRegistered := true;
  134.   pxModuleText := txloper.Create;
  135.   pxProcedure := txloper.Create;
  136.   pxTypetext := txloper.Create;
  137.   pxFunctiontext := txloper.Create;
  138.   pxArgumentText := txloper.Create;
  139.   pxMacroType := txloper.Create;
  140.   pxCategory := txloper.Create;
  141.   pxShortCutText := txloper.Create;

  142.   UDF_Id := Register_UDF;

  143.   pxShortCutText.Free;
  144.   pxCategory.free;
  145.   pxMacroType.free;
  146.   pxArgumentText.free;
  147.   pxFunctiontext.free;
  148.   pxTypetext.free;
  149.   pxProcedure.free;
  150.   pxModuleText.free;
  151. end;

  152. // Removes all Registered Functions

  153. procedure UnRegister_All;
  154. var
  155.   Module: txloper;

  156.   procedure DeRegister(Id: Integer);
  157.   begin
  158.     Module.SetNum(Id);
  159.     EResult := Excel4V(xlfunregister, @res, 1, [Module.thelpxloper]);
  160.     asm
  161.         pop     sink;
  162.     end;                                     // Never Remove
  163.   end;

  164. begin
  165.   Module := txloper.Create;
  166.   DeRegister(UDF_Id);
  167.   Module.Free;
  168. end;

  169. function xlAutoClose: integer; stdcall;
  170. begin
  171.   Unregister_All;
  172.   result := 1;
  173. end;

  174. function xlAutoOpen: integer; stdcall;
  175. begin
  176.   Register_All;
  177.   result := 1;
  178. end;

  179. function xlAddInManagerInfo(xl: lpxloper): lpxloper; stdcall;
  180. var
  181.   xint, xintval: xloper;
  182. begin
  183.   xint.xltype := xltypeint; // Always used to specify type of input
  184.   xint.val.w := xltypeInt;                   // Conversion type is set here
  185.   EResult := Excel4V(xlcoerce, @xintval, 2, [xl, @xint]);
  186.   asm
  187.         pop     sink;
  188.   end;                                       // Never Remove
  189.   if (xintval.val.w = 1) then
  190.   begin
  191.     res.xltype := xltypestr;
  192.     res.val.str := @DLLversion;
  193.   end
  194.   else
  195.   begin
  196.     res.xltype := xltypeerr;
  197.     res.val.err := 15;
  198.   end;
  199.   result := @res;
  200. end;

  201. function xlAutoRegister(pXName: lpxloper): lpxloper; stdcall;
  202. begin
  203.   Result := @res;
  204. end;

  205. function xlAutoRemove: integer; stdcall;
  206. begin
  207. //  MessageBox(GetActiveWindow, 'MyXll卸载成功', '提示', MB_ICONINFORMATION or MB_OK);
  208.   // Tidy Up code here
  209.   result := 1;
  210. end;

  211. function xlAutoAdd: integer; stdcall;
  212. begin
  213.   Register_All;
  214. //  MessageBox(GetActiveWindow, 'MyXll安装成功', '提示', MB_ICONINFORMATION or MB_OK);
  215.   result := 1;
  216. end;

  217. procedure xlAutoFree(ramptr: lpxloper); stdcall;
  218. begin
  219.   Freemem(ramptr);
  220. end;

  221. constructor txloper.create;
  222. begin
  223.   inherited Create;
  224.   fillchar(factualStr, sizeof(fActualStr), 0);
  225.   fillchar(fxloper, sizeof(fxloper), 0);
  226. end;

  227. destructor txloper.Destroy;
  228. begin
  229.   inherited Destroy;
  230. end;

  231. constructor txloper.Create_Str(NewStr: ShortString);
  232. begin
  233.   inherited Create;
  234.   fillchar(fxloper, sizeof(fxloper), 0);
  235.   fillchar(factualstr, sizeof(factualstr), 0);
  236.   SetStr(NewStr);
  237. end;

  238. procedure txloper.SetStr(NewStr: ShortString);
  239. begin
  240.   fillchar(factualstr, sizeof(factualstr), 0);
  241.   factualstr := NewStr;
  242.   fxloper.xltype := xlTypeStr;
  243.   fxloper.val.Str := addr(fActualStr);
  244. end;

  245. procedure txloper.SetErr;
  246. begin
  247.   fxloper.xltype := xltypEerr;
  248.   fxloper.val.err := xlerrvalue;
  249. end;

  250. procedure txloper.SetNum(NewNumber: Integer);
  251. begin
  252.   fxloper.xltype := xltypeNum;
  253.   fxloper.val.num := NewNumber;
  254. end;

  255. procedure txloper.SetInt(NewNumber: Integer);
  256. begin
  257.   fxloper.xltype := xltypeInt;
  258.   fxloper.val.num := NewNumber;
  259. end;

  260. function txloper.Getlpxloper: lpxloper;
  261. begin
  262.   result := addr(fxloper);
  263. end;

  264. exports
  265.   // Excel Recognition functions
  266.   xlAutoFree,
  267.   xlAutoAdd,
  268.   xlAutoOpen,
  269.   xlAutoClose,
  270.   xlAutoRemove,
  271.   xlAutoRegister,
  272.   xlAddInManagerInfo,
  273.   // Exported Invisible Functions
  274.   UDF_Add;

  275. begin
  276.   HaveRegistered := false;
  277. end.
复制代码

MyExcelXll_For_Win32.rar

49.79 KB, 下载次数: 29

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-4 09:53 , Processed in 0.024867 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表