举个例子,一个Delphi常用的业务逻辑,数据导出到Excel,完全可以写成一个公用的模块放置在业务单元,子窗体用到时直接调用即可,但是一般情况下,事情并不止想象的那么简单,维护人员的思想真的一言难尽。
1 library SuperDll;
2
3 { Important note about DLL memory management: ShareMem must be the
4 first unit in your library's USES clause AND your project's (select
5 Project-View Source) USES clause if your DLL exports any procedures or
6 functions that pass strings as parameters or function results. This
7 applies to all strings passed to and from your DLL--even those that
8 are nested in records and classes. ShareMem is the interface unit to
9 the BORLNDMM.DLL shared memory manager, which must be deployed along
10 with your DLL. To avoid using BORLNDMM.DLL, pass string information
11 using PChar or ShortString parameters. }
12
13 uses
14 SysUtils, Classes, Variants, Graphics, Controls, IdBaseComponent, IdComponent, IdFTP,
15 IdFTPCommon, IdTCPConnection, IdTCPClient, IdMessage, IdMessageClient, IdSMTP, cxGrid,
16 cxGridExportLink, ComObj,
17 cxCustomData, cxGraphics,
18 cxData, cxDataStorage, cxEdit, cxDBData, cxGridLevel,
19 cxClasses, cxControls, cxGridCustomView, cxGridCustomTableView,
20 cxGridTableView, cxGridDBTableView;
21
22 {$R *.res}
23
24 function SuperDll_Init: Boolean; stdcall;
25 begin
26 Result := True;
27 end;
28
29 function SuperDll_Ftp_PutOrGet(vType: string; var vUserName: string; var vPassWord: string; var vHost: string; var vDir: string; var vDesFilePath: string; vSouFilePath: string): Boolean; stdcall; //2: FTP文件上传下载
30 var
31 IdFtp: TIdFTP;
32 begin
33 IdFtp := TIdFTP.Create(nil);
34 Result := False;
35 try
36 with IdFtp do
37 begin
38 if Connected then Disconnect;
39 Username := vUserName;
40 Password := vPassWord;
41 Host := vHost;
42 Port := 21;
43 Connect;
44 ChangeDir(vDir);
45 TransferType := ftBinary;
46 if vType = 'Put' then
47 begin
48 Put(vSouFilePath, ExtractFileName(vSouFilePath));
49 end
50 else if vType = 'Get' then
51 begin
52 Get(ExtractFileName(vDesFilePath), vDesFilePath, True);
53 end;
54 end;
55 finally
56 IdFtp.Disconnect;
57 IdFtp.Free;
58 Result := True;
59 end;
60 end;
61
62 function SuperDll_EMail_Send(vSubject: string; var vFrom: string; var vRecipients: string; var vCCList: string; var vBccList: string; var vBody: string; var vAttachment: string; var vUsername: string; var vPassword: string; var vHost: string): Boolean; stdcall;
63 var
64 IdSMTP: TIdSMTP;
65 IdMessage: TIdMessage;
66 begin
67 Result := False;
68 IdSMTP := TIdSMTP.Create(nil);
69 IdMessage := TIdMessage.Create(nil);
70 try
71 with IdMessage do
72 begin
73 Clear;
74 Subject := vSubject;
75 From.Text := vFrom;
76 Recipients.EMailAddresses := vRecipients;
77 CCList.EMailAddresses := vCCList;
78 BccList.EMailAddresses := vBccList;
79 Priority := TIdMessagePriority(4);
80 if Trim(vAttachment) <> '' then
81 begin
82 TIdAttachment.Create(MessageParts, Trim(vAttachment));
83 end;
84 vBody := vBody + #13#10;
85 vBody := vBody + #13#10;
86 vBody := vBody + #13#10;
87 vBody := vBody + #13#10;
88 vBody := vBody + 'It is Auto Mail System,please do not reply this mail directly,thank you!';
89 Body.Add(vBody);
90 end;
91
92 with IdSMTP do
93 begin
94 if Connected then Disconnect;
95 AuthenticationType := atLogin;
96 Port := 25;
97 UserName := vUsername;
98 Password := vPassword;
99 Host := vHost;
100 Connect;
101 end;
102
103 IdSMTP.Send(IdMessage);
104 IdSMTP.Disconnect;
105
106 Result := True;
107 finally
108 IdSMTP.Free;
109 IdMessage.Free;
110 end;
111 end;
112
113 function SaveCxGridToExcel(vCxGrid: TcxGrid; var vFullPathName: string): Boolean; stdcall;
114 begin
115 Result := False;
116 vCxGrid := TcxGrid.Create(nil);
117 ExportGridToExcel(vFullPathName, vCxGrid);
118 vCxGrid.Free;
119 Result := True;
120 end;
121
122 function SaveCxGridToCSV(vCxGrid: TcxGrid; var vFullPathName: string): Boolean; stdcall;
123 begin
124 Result := False;
125
126 ExportGridToText(vFullPathName + '.XLS', vCxGrid, True, True, ',', '', '', 'CSV');
127 Result := True; ;
128 end;
129
130 exports
131 SuperDll_Init,
132 SuperDll_Ftp_PutOrGet,
133 SuperDll_EMail_Send,
134 SaveCxGridToExcel,
135 SaveCxGridToCSV;
136
137 begin
138 end.