给我加分:
(我的DB2Excel类)
unit Excels;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,Dbtables;
type
TEPExcel=class
private
VExcel:variant;{Excel对象变量}
//FileName:string;{文件名}
public
constructor Create(FName:string);
procedure SetCells(CPosition:string;CValue:Variant;FontSize:integer);
procedure SetRange(rv:string;sx,ex,sy,ey,size:integer);
procedure Vsingle(var vquery:tquery;strqury,cc:string;fz:integer);
procedure Setvquary(var equery:Tquery;xs,ys,fs:integer);
procedure Cellmerge(cs,ce,ns,ne,fontsize:integer);
procedure Printpreview();
procedure Print();
procedure Save();
procedure Protect(password:string);
procedure WSCopy;
procedure PreviewBooks;
procedure Unprotect(pw:string);
procedure Unprotectworkbook(pwd:string);
end;
implementation
uses Comobj;
{创建一个Excel对象变量}
Constructor TEPExcel.Create(FName:string);
begin
VExcel:=CreateOleObject('Excel.Application');
VExcel.WorkBooks.Open(FName);
end;
{设置一个单元格属性}
procedure TEPExcel.SetCells(CPosition:string;CValue:Variant;FontSize:integer);
begin
VExcel.Range[CPosition].Font.Size:=FontSize;
VExcel.Range[CPosition].Font.Bold:=True;
VExcel.Range[CPosition].Value:=CValue;
end;
{设置区域属性}
procedure TEPExcel.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);
VExcel.Range[CP].Font.Bold:=True;
VExcel.Range[CP].Font.Size:=Size;
VExcel.Range[CP].Value:=rv;
end
end;
end;
procedure TEPExcel.setvquary(var equery:Tquery;xs,ys,fs:integer);
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:=dabname;
//equery.TableName:=tabname;
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);
VExcel.range[rangs].font.bold:=true;
VExcel.range[rangs].font.size:=fs;
VExcel.range[rangs].value:=equery.fields[i-xs].asstring;
end;
equery.next;
end;
equery.Destroy;
end;
{合并单元格}
procedure TEPExcel.CellMerge(cs,ce,ns,ne,fontsize:integer);
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);
VExcel.application.DisplayAlerts:=False;
VExcel.range[scell].merge;
VExcel.range[scell].font.size:=fontsize;
VExcel.range[scell].font.bold:=true;
VExcel.range[scell].WrapText:=True;
VExcel.application.DisplayAlerts:=true;
end;
{打印}
procedure TEPExcel.print();
begin
VExcel.range['b5'].activate;
VExcel.application.visible:=true;
VExcel.Worksheets['Sheet1'].PageSetup.Orientation:=2;
VExcel.Worksheets['Sheet1'].printout;
end;
{打印预览}
procedure TEPExcel.printpreview();
begin
VExcel.range['b5'].activate;
VExcel.application.visible:=true;
VExcel.Worksheets['Sheet1'].PageSetup.Orientation:=2;
VExcel.Worksheets['Sheet1'].printpreview;
end;
{存盘}
procedure TEPExcel.save();
begin
VExcel.ActiveWorkbook.Save
end;
{表保护}
procedure TEPExcel.protect(password:string);
begin
VExcel.worksheets['sheet1'].protect(password,true,true,true);
end;
procedure TEPExcel.Vsingle(var vquery:tquery;strqury,cc:string;fz:integer);
begin
vquery.close;
vquery.SQL.Clear;
vquery.SQL.add(strqury);
vquery.open;
VExcel.range[cc].value:=vquery.fields[0].asstring;
end;
procedure TEPExcel.WSCopy;
begin
VExcel.WorkSheets['Sheet1'].Copy(After:=VExcel.WorkSheets[VExcel.WorkSheets.Count]);
VExcel.WorkSheets[VExcel.Worksheets.Count].Protect('aa',true,true,true);
VExcel.workSheets['Sheet1'].Activate;
end;
procedure TEPExcel.PreviewBooks;
begin
VExcel.Application.Visible:=True;
VExcel.Application.ActiveWorkBook.PrintOut(Copies:=1, Preview:=True, Collate:=True );
end;
procedure TEPExcel.UnProtect(pw:string);
var
I:Integer;
begin
for I:=1 to VExcel.WorkSheets.Count do
begin
VExcel.WorkSheets.Select;
VExcel.ActiveSheet.UnProtect(pw);
end;
end;
{解除单元格保护}
procedure TEPExcel.UnProtectworkbook(pwd:string);
begin
VExcel.activeworkbook.unprotect(pwd);
end;
end.