📄 modeexcelunit.pas
字号:
unit ModeExcelUnit;
interface
uses
Classes, windows, SysUtils,Forms,ADODB,Dialogs,Variants,ActiveX;
type
DoExcel = class(TThread)
SentMsg : String; //传递进程信息
I1 : Integer; //总数
I2 : Integer; //进度数
ADODst_1: TADODataSet;
procedure postMessages; //传递信息
procedure QueryMessage; //显示过程
procedure DoOutData; //导出数据
private
{ Private declarations }
protected
procedure Execute; override;
function Convert(intt:integer):string;
public
constructor Create(Runing:Boolean);
end;
implementation
uses Progress, Comobj,DrawInfor, GlobalVarDefs;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure DoExcel.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ DoExcel }
constructor DoExcel.Create(Runing: Boolean);
begin
SentMsg:='正在生成Excel模板,请稍候…………';
Synchronize(postMessages);
ProgressForm.RzProgressBar1.Percent:=0;
inherited Create(Not Runing);
end;
procedure DoExcel.DoOutData;
var
Exls,sheet : variant;
fieldNum,I,j : integer;
lie:string;
begin
FieldNum:=11;//字段数量
try
Exls:=createoleobject('Excel.application');
sheet:=createoleobject('Excel.sheet');
except
Messagebox(handle,'未发现系统中安装了Microsoft Excel,在使用该功能前,请先将其安装!','错误',mb_ok+mb_iconwarning);
Exit;
end;
lie:=Convert(FieldNum+1);//转换
try
sheet:=Exls.workBooks.add;
try
I1:=FieldNum;
Exls.worksheets[1].Range['A3:'+lie+inttostr(I1+3)].NumberFormatLocal:='@' ;
Exls.Cells[1,1].ColumnWidth:=8;
Exls.Cells[1,2].ColumnWidth:=10;
Exls.Cells[1,3].ColumnWidth:=10;
Exls.Cells[1,4].ColumnWidth:=10;
Exls.Cells[1,5].ColumnWidth:=10;
Exls.Cells[1,6].ColumnWidth:=15;
Exls.Cells[1,7].ColumnWidth:=15;
Exls.Cells[1,8].ColumnWidth:=15;
Exls.Cells[1,9].ColumnWidth:=15;
Exls.Cells[1,10].ColumnWidth:=15;
SentMsg:='正在生成Excel模板,请稍候…………';
Synchronize(postMessages);
i:=1;
for i:=1 to FieldNum do begin
I2:=I;
Synchronize(QueryMessage);
exls.Cells[1,I] :=ModeExcel[I];
Exls.Cells[1,i].Font.Size:=10;
end;
Exls.Cells[1,1].ColumnWidth:=10;
Exls.Cells[1,2].ColumnWidth:=11;
Exls.Cells[1,3].ColumnWidth:=11;
Exls.Cells[1,4].ColumnWidth:=11;
Exls.Cells[1,5].ColumnWidth:=16;
Exls.Cells[1,6].ColumnWidth:=10;
Exls.Cells[1,7].ColumnWidth:=11;
Exls.Cells[1,8].ColumnWidth:=13;
Exls.Cells[1,9].ColumnWidth:=13;
Exls.Cells[1,10].ColumnWidth:=10;
Exls.Cells[1,11].ColumnWidth:=13;
Exls.worksheets[1].Rows[1].Font.bold:=true;
//显示Excel
try
sleep(1000);
Exls.visible:=true;
except
end;
except
Messagebox(handle,'生成Excel模板异常,请稍后再试!','错误',MB_OK+MB_ICONERROR);
Exit;
end;
except
Messagebox(handle,'生成Excel模板异常,请稍后再试!!','错误',mb_ok+mb_iconerror);
end;
end;
procedure DoExcel.Execute;
begin
{ Place thread code here }
try
CoInitialize(nil);
DoOutData;
finally
Progressform.Close;
end;
end;
procedure DoExcel.postMessages;
begin
ProgressForm.RzGroupBox1.Caption:=SentMSG;
end;
procedure DoExcel.QueryMessage;
begin
progressForm.RzProgressBar1.Percent:=round(100*(i2/i1));
end;
function DoExcel.Convert(intt:integer):string;
var
lie: string;
begin
case intt of
2: lie:='B';
3: lie:='C';
4: lie:='D';
5: lie:='E';
6: lie:='F';
7: lie:='G';
8: lie:='H';
9: lie:='I';
10: lie:='J';
11: lie:='K';
12: lie:='L';
13: lie:='M';
14: lie:='N';
15: lie:='O';
16: lie:='P';
17: lie:='Q';
18: lie:='R';
19: lie:='S';
20: lie:='T';
21: lie:='U';
22: lie:='V';
23: lie:='W';
24: lie:='X';
25: lie:='Y';
26: lie:='Z';
27: lie:='AA';
28: lie:='AB';
29: lie:='AC';
30: lie:='AD';
31: lie:='AE';
32: lie:='AF';
33: lie:='AG';
34: lie:='AH';
35: lie:='AI';
36: lie:='AJ';
37: lie:='AK';
38: lie:='AL';
49: lie:='AM';
40: lie:='AN';
41: lie:='AO';
42: lie:='AP';
43: lie:='AQ';
44: lie:='AR';
45: lie:='AS';
46: lie:='AT';
47: lie:='AU';
48: lie:='AV';
end;
result:= lie;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -