unit U_myexcel;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleServer, Excel2000, DB, DBTables,ComObj,registry,
Buttons;
type
TF_myexcel = class(TDataModule)
ExcelApplication1: TExcelApplication;
private
public
{ Public declarations }
end;
var
Fexcel:Variant;
F_myexcel: TF_myexcel;
function GetRegString(RootKey: HKEY; Section, Key, DefaultValue: string):string; // 读取注册表键值
function Openexcel: Boolean; //尝试打开excel
procedure SetCells(CPosition:string;CValue:Variant;FontSize:integer); {设置一个单元格属性}
procedure SetRange(rv:string;sx,ex,sy,ey,size:integer);{设置区域属性}
procedure setvquary(var equery:Tquery;xs,ys,fs:integer); //设置数据库
procedure input_totable(var etable:Ttable;xs,xs2,ys,ys2,fs:integer); //输入table
procedure CellMerge(cs,ce,ns,ne,fontsize:integer); {合并单元格}
procedure print(); {打印}
procedure printpreview(); {打印预览}
procedure save();{存盘}
procedure protect(password:string); {表保护}
procedure Vsingle(var vquery:tquery;strqury,cc:string;fz:integer); //设置数据库
procedure WSCopy; //表拷贝
procedure PreviewBooks; //预览
procedure UnProtect(pw:string); //解除保护
procedure UnProtectworkbook(pwd:string); {解除单元格保护}
implementation
//@@@@@@@@@@@@@@@@@
function GetRegString(RootKey: HKEY; Section, Key, DefaultValue: string):string;
var
MyReg: TRegistry;
begin
Result := DefaultValue;
MyReg := TRegistry.Create;
if MyReg <> nil then
begin
MyReg.RootKey := RootKey;
if MyReg.OpenKeyReadOnly(Section) then
begin
Result :=MyReg.ReadString(Key);
if Result = '' then Result := DefaultValue;
MyReg.CloseKey;
end;
MyReg.Free;
end;
end;
//&&&&&&&&&&&&&&&&&
//****************
function Openexcel: Boolean;
var
Curexcel: string;
roro:boolean;
rororo:boolean;
begin
roro:=true;
rororo:=true;
Curexcel := GetRegString(HKEY_CLASSES_ROOT, '/excel.Application/CurVer', '', '');
if Curexcel = '' then
begin
Application.MessageBox('请先安装 excel 97 或 excel 2000!或 excel XP 或 excel 2003',
PChar('警告'), MB_OK + MB_ICONSTOP);
Result := False;
Exit;
end;
if (Curexcel <> 'excel.Application.8') and (Curexcel <> 'excel.Application.9') then
begin
Curexcel := GetRegString(HKEY_CLASSES_ROOT, '/excel.Application.10/CLSID', '', '');
if Curexcel <> '' then Curexcel := 'excel.Application.10';
if Curexcel = '' then
begin
roro:=false;
end;
end;
if (Curexcel <> 'excel.Application.8') and (Curexcel <> 'excel.Application.9') and (Curexcel <> 'excel.Application.10') then
begin
Curexcel := GetRegString(HKEY_CLASSES_ROOT, '/excel.Application.11/CLSID', '', '');
if Curexcel <> '' then Curexcel := 'excel.Application.11';
if Curexcel = '' then
begin
rororo:=false ;
end;
end;
if (roro=false) and (rororo=false) then
begin
Application.MessageBox('无法运行 excel 97 或 excel 2000,' + #13#10
+ '请检查 excel 97 或 excel 2000 是否已经正确安装。',
PChar('警告!'), MB_OK + MB_ICONSTOP);
Result := False;
Exit;
end;
try
Fexcel:= CreateOleObject(Curexcel);
if VarIsEmpty(Fexcel) then
begin
Application.MessageBox('无法运行 excel 97 或 excel 2000,' + #13#10
+ '请检查 excel 97 或 excel 2000 是否已经正确安装。',
PChar('警告!'), MB_OK + MB_ICONSTOP);
Result := False;
Exit;
end;
except
Application.MessageBox('无法运行 excel 97 或 excel 2000,' + #13#10
+ '请检查 excel 97 或 excel 2000 是否已经正确安装。',
PChar('警告!'), MB_OK + MB_ICONSTOP);
Result := False;
Exit;
end;
try
begin
Fexcel.DisplayAlerts := False;
Fexcel.workbooks.Open(ExtractFilePath(Application.EXEName)+'my.xls');
Fexcel.caption:='excel示例(小红河)';
Fexcel.Visible := True;
Result := True;
end;
except
Application.MessageBox('分析失败,',
PChar('警告3!'), MB_OK + MB_ICONSTOP);
Result := False;
end;
{
Fexcel.Visible := True; // 1) 显示当前窗口:
Fexcel.Caption := '应用程序调用 Microsoft Excel'; //2) 更改 Excel 标题栏:
Fexcel.WorkBooks.Add; //3) 添加新工作簿:
Fexcel.WorkBooks.Open( 'C:/Excel/Demo.xls' ); //4) 打开已存在的工作簿:
Fexcel.WorkSheets[2].Activate; //5) 设置第2个工作表为活动工作表:
Fexcel.WorksSheets[ 'Sheet2' ].Activate; //或
Fexcel.Cells[1,4].Value := '第一行第四列'; //6) 给单元格赋值:
Fexcel.ActiveSheet.Columns[1].ColumnsWidth := 5; //7) 设置指定列的宽度(单位:字符个数),以第一列为例:
Fexcel.ActiveSheet.Rows[2].RowHeight := 1/0.035; // 1厘米 //8) 设置指定行的高度(单位:磅)(1磅=0.035厘米),以第二行为例:
Fexcel.WorkSheets[1].Rows[8].PageBreak := 1; //9) 在第8行之前插入分页符:
Fexcel.ActiveSheet.Columns[4].PageBreak := 0; //10) 在第8列之前删除分页符:
Fexcel.ActiveSheet.Range[ 'B3
4' ].Borders[2].Weight := 3; //11) 指定边框线宽度://1-左 2-右 3-顶 4-底 5-斜( / ) 6-斜( / )
Fexcel.ActiveSheet.Cells[1,4].ClearContents; //12) 清除第一行第四列单元格公式:
Fexcel.ActiveSheet.Rows[1].Font.Name := '隶书'; //13) 设置第一行字体属性:
Fexcel.ActiveSheet.Rows[1].Font.Color := clBlue;
Fexcel.ActiveSheet.Rows[1].Font.Bold := True;
Fexcel.ActiveSheet.Rows[1].Font.UnderLine := True;
//14) 进行页面设置:
Fexcel.ActiveSheet.PageSetup.CenterHeader := '报表演示'; //a.页眉:
Fexcel.ActiveSheet.PageSetup.CenterFooter := '第&P页'; //b.页脚:
Fexcel.ActiveSheet.PageSetup.HeaderMargin := 2/0.035; //c.页眉到顶端边距2cm:
Fexcel.ActiveSheet.PageSetup.HeaderMargin := 3/0.035; //d.页脚到底端边距3cm:
Fexcel.ActiveSheet.PageSetup.TopMargin := 2/0.035; //e.顶边距2cm:
Fexcel.ActiveSheet.PageSetup.BottomMargin := 2/0.035; //f.底边距2cm:
Fexcel.ActiveSheet.PageSetup.LeftMargin := 2/0.035; //g.左边距2cm:
Fexcel.ActiveSheet.PageSetup.RightMargin := 2/0.035; //h.右边距2cm:
Fexcel.ActiveSheet.PageSetup.CenterHorizontally := 2/0.035; //i.页面水平居中:
Fexcel.ActiveSheet.PageSetup.CenterVertically := 2/0.035; //j.页面垂直居中:
Fexcel.ActiveSheet.PageSetup.PrintGridLines := True; //k.打印单元格网线:
//15) 拷贝操作:
Fexcel.ActiveSheet.Used.Range.Copy; //a.拷贝整个工作表:
Fexcel.ActiveSheet.Range[ 'A1:E2' ].Copy; //b.拷贝指定区域: 好像不好使
Fexcel.ActiveSheet.Range[ 'A1:E2' ].PasteSpecial; //c.从A1位置开始粘贴:
Fexcel.ActiveSheet.Range.PasteSpecial; //d.从文件尾部开始粘贴:
Fexcel.ActiveSheet.Rows[2].Insert; //16) 插入一行或一列:
Fexcel.ActiveSheet.Columns[1].Insert; //16) 插入一行或一列
Fexcel.ActiveSheet.Rows[2].Delete; //17) 删除一行或一列:
Fexcel.ActiveSheet.Columns[1].Delete; //17) 删除一行或一列:
Fexcel.ActiveSheet.PrintPreview; //18) 打印预览工作表:
Fexcel.ActiveSheet.PrintOut; //19) 打印输出工作表:
if not Fexcel.ActiveWorkBook.Saved then //20) 工作表保存:
Fexcel.ActiveSheet.PrintPreview;
Fexcel.SaveAs( 'C:/Excel/Demo1.xls' ); //21) 工作表另存为:
Fexcel.ActiveWorkBook.Saved := True; //22) 放弃存盘:
//23) 关闭工作簿:
Fexcel.WorkBooks.Close;
//24) 退出 Excel:
//Fexcel.Quit; }
end;
//@@@@@@@@@@@@@@@
{设置一个单元格属性}
procedure SetCells(CPosition:string;CValue:Variant;FontSize:integer);
begin
Fexcel.Range[CPosition].Font.Size:=FontSize;
Fexcel.Range[CPosition].Font.Bold:=True;
Fexcel.Range[CPosition].Value:=CValue;
end;
{设置区域属性}
procedure SetRange(rv:string;sx,ex,sy,ey,size:integer);
const
{Excel的列标志号}
RowNo:array[1..26] of string=
('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q',
'R','S','T','U','V','W','X','Y','Z');
var
X:Integer;
Y:Integer;
CP:string;{单元格位置}
begin
for Y:=sy to ey do
begin
for X:=sx to ex do
begin
CP:=RowNo[X]+IntToStr(Y);
Fexcel.Range[CP].Font.Bold:=True;
Fexcel.Range[CP].Font.Size:=Size;
Fexcel.Range[CP].Value:=rv;
end
end;
end;
procedure setvquary(var equery:Tquery;xs,ys,fs:integer); //xs:从excel的xs列开始,ys:从cecel的ys行开始,fs:字体
const
crs:array[1..26] of string=('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
var
rangs:string;
i:integer;
j:integer;
begin
//equery.DatabaseName:=''; 使用时应该有。
//equery.TableName:='';
equery.first;
for j:=ys to equery.recordcount-1+ys do
begin
for i:=xs to equery.fieldcount-1+xs do
begin
rangs:=crs
+inttostr(j);
Fexcel.range[rangs].font.bold:=true;
Fexcel.range[rangs].font.size:=fs;
Fexcel.range[rangs].value:=equery.fields[i-xs].asstring;
end;
equery.next;
end;
equery.Destroy;
end;
{合并单元格}
procedure CellMerge(cs,ce,ns,ne,fontsize:integer); //cs,ce:列开始与结束点,ns,ne:行开始与结束点.fontsize在excel里默认的是12;
const
cn:array[1..26] of string=('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
var
scell:string;
begin
scell:=cn[cs]+inttostr(ns)+':'+cn[ce]+inttostr(ne);
Fexcel.application.DisplayAlerts:=False;
Fexcel.range[scell].merge;
Fexcel.range[scell].font.size:=fontsize;
Fexcel.range[scell].font.bold:=true;
Fexcel.range[scell].WrapText:=True;
Fexcel.application.DisplayAlerts:=true;
end;
{打印}
procedure print();
begin
Fexcel.range['b5'].activate;
Fexcel.application.visible:=true;
Fexcel.Worksheets['Sheet1'].PageSetup.Orientation:=2;
Fexcel.Worksheets['Sheet1'].printout;
end;
{打印预览}
procedure printpreview();
begin
Fexcel.range['b5'].activate;
Fexcel.application.visible:=true;
Fexcel.Worksheets['Sheet1'].PageSetup.Orientation:=2;
Fexcel.Worksheets['Sheet1'].printpreview;
end;
{存盘}
procedure save();
begin
Fexcel.ActiveWorkbook.Save
end;
{表保护}
procedure protect(password:string);
begin
Fexcel.worksheets['sheet1'].protect(password,true,true,true);
end;
procedure Vsingle(var vquery:tquery;strqury,cc:string;fz:integer); //cc:表示单元格,如A5,///等..fz:字体
begin
vquery.close;
vquery.SQL.Clear;
vquery.SQL.add(strqury);
vquery.open;
Fexcel.range[cc].value:=vquery.fields[0].asstring;
end;
procedure WSCopy;
begin
Fexcel.WorkSheets['Sheet1'].Copy(After:=Fexcel.WorkSheets[Fexcel.WorkSheets.Count]);
Fexcel.WorkSheets[Fexcel.Worksheets.Count].Protect('aa',true,true,true);
Fexcel.workSheets['Sheet1'].Activate;
end;
procedure PreviewBooks;
begin
Fexcel.Application.Visible:=True;
Fexcel.Application.ActiveWorkBook.PrintOut(Copies:=1, Preview:=True, Collate:=True );
end;
procedure UnProtect(pw:string);
var
I:Integer;
begin
for I:=1 to Fexcel.WorkSheets.Count do
begin
Fexcel.WorkSheets.Select;
Fexcel.ActiveSheet.UnProtect(pw);
end;
end;
{解除单元格保护}
procedure UnProtectworkbook(pwd:string);
begin
Fexcel.activeworkbook.unprotect(pwd);
end;
procedure input_totable(var etable: Ttable; xs,xs2, ys,ys2, fs: integer); //从xs列,ys行开始的xs2+1行和ys2+1列均导入数据库
const
crs:array[1..26] of string=('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
var
rangs:string;
i,ii:integer;
j,jj:integer;
begin
//equery.DatabaseName:=''; 使用时应该有。
//equery.TableName:='';
etable.first;
for j:=ys to ys2+ys do
begin
etable.Append;
for i:=xs to xs2+xs do
begin
rangs:=crs+inttostr(j);
etable.fields[i-xs].asstring:=Fexcel.range[rangs].value;
end;
etable.next;
end;
// etable.Destroy;
end;
//@@@@@@@@@@@@@@@@
{$R *.dfm}
{ TF_myexcel }
end.