首页 > 代码库 > [转]Delphi导出Excel的设置操作
[转]Delphi导出Excel的设置操作
//设置页边距:
ExlApp.ActiveSheet.PageSetup.TopMargin:=1.5/0.035;
ExlApp.ActiveSheet.PageSetup.BottomMargin:=1.5/0.035;
ExlApp.ActiveSheet.PageSetup.LeftMargin:=1/0.035;
ExlApp.ActiveSheet.PageSetup.RightMargin:=1/0.035;
ExlApp.ActiveSheet.PageSetup.HeaderMargin:=0.5/0.035;
ExlApp.ActiveSheet.PageSetup.FooterMargin:=0.5/0.035;
//设置页面对齐方式
ExlApp.ActiveSheet.PageSetup.CenterHorizontally:=True; //页面水平居中
// ExlApp.ActiveSheet.PageSetup.CenterVertically :=True; //页面垂直居中
//设置整体字体格式
ExlApp.Cells.Font.Name:=‘宋体‘; //字体
ExlApp.Cells.Font.Size:=12; //字号
ExlApp.Cells.RowHeight:=16; //行高
ExlApp.Cells.VerticalAlignment:=2; //垂直方向对齐方式:居中
end;{CreatRepSheet}
procedure SetAddMess(H_Mess1,H_Mess2,H_Mess3,F_Mess1,F_Mess2,F_Mess3:String);
//用户自定义页眉、页脚(即:页标题、页号)
begin
ExlApp.ActiveSheet.PageSetup.LeftHeader:=H_Mess1;
ExlApp.ActiveSheet.PageSetup.CenterHeader:=H_Mess2;
ExlApp.ActiveSheet.PageSetup.RightHeader:=H_Mess3;
end;{SetAddMess}
procedure SetRepBody(x,ch:Integer;cw:Double;cf:String);
//设置整体各列数据格式
begin
ExlApp.ActiveSheet.Columns[x].ColumnWidth:=cw; //列宽
ExlApp.ActiveSheet.Columns[x].NumberFormat:=Cf; //单元格数据格式
ExlApp.ActiveSheet.Columns[x].HorizontalAlignment:=ch; //水平方向对齐方式
end;{SetRepBody}
procedure CreatTitle(TitleName:String;y:Integer);
{设置标题}
Var
RepSpace:String;
begin
CellMerge(1,1,1,y);
ExlApp.cells(1,1):=TitleName;
RepSpace:=‘A1‘+‘:‘+GetRepRange(1,y);
ExlApp.Range[RepSpace].Select;
ExlApp.Selection.NumberFormat :=‘G/通用格式‘;
ExlApp.Selection.Font.Size:=22;
ExlApp.Selection.Font.Name:=‘黑体‘;
ExlApp.Selection.Font.Bold:=True;
ExlApp.Selection.HorizontalAlignment:=3; //水平方向对齐方式:居中
ExlApp.Rows[1].RowHeight:=28;
end;{RepHead}
procedure CreatSubHead(SubTitle: Array of String);
{设置常规子表头}
Var
i,j:Integer;
begin
j:=0;
for i:=Low(SubTitle) to High(SubTitle) do
begin
Inc(j);
ExlApp.cells(2,j):=SubTitle[i];
end;
end;{CreatRepHead}
procedure SubHeadFormat(y,r:Integer);
{设置子表头格式}
Var
RepSpace:String;
n:Integer;
begin
RepSpace:=‘A2‘+‘:‘+GetRepRange(1+r,y);
ExlApp.Range[RepSpace].Select;
ExlApp.Selection.NumberFormat :=‘G/通用格式‘;
ExlApp.Selection.HorizontalAlignment:=3; //表头水平对齐方式:居中
ExlApp.Selection.Font.Bold:=True;
for n:=1 to r do
begin
ExlApp.Rows[1+n].RowHeight:=18;
SetRepLine(1+n,y);
end;{for}
end;{SubHeadFormat}
procedure DTSubHeadGS(x,y,r:Integer);
{设置动态子表头格式}
Var
RepSpace:String;
n:Integer;
begin
RepSpace:=GetRepRange(x,1)+‘:‘+GetRepRange(x+r-1,y);
ExlApp.Range[RepSpace].Select;
ExlApp.Selection.NumberFormat :=‘G/通用格式‘;
ExlApp.Selection.HorizontalAlignment:=3; //表头水平对齐方式:居中
ExlApp.Selection.Font.Bold:=True;
for n:=0 to r-1 do
begin
ExlApp.Rows[x+n].RowHeight:=18;
SetRepLine(x+n,y);
end;{for}
end;{DTSubHeadGS}
procedure WriteData(RepData: String; x,y,flag:Integer); //写入数据
{写数据}
begin
if flag=1 then
ExlApp.cells(x,y):=StrToDate(RepData)
else
ExlApp.cells(x,y):=RepData;
end;{WriteDate}
procedure RepPageBreak(x,y,r:Integer); //X:分页处行数,Y:列数,R:子表头总共的行数
//分页、复制表头
Var
RepSpace:String;
n:Integer;
begin
ExlApp.ActiveSheet.Rows[x].PageBreak := 1;
RepSpace:=‘A1‘+‘:‘+GetRepRange(r+1,y);
ExlApp.ActiveSheet.Range[RepSpace].Copy;
RepSpace:=‘A‘+IntToStr(x);
ExlApp.ActiveSheet.Range[RepSpace].PasteSpecial;
ExlApp.Rows[x].RowHeight:=28;
for n:=2 to r do
ExlApp.Rows[x+n].RowHeight:=18;
end;{RepPageBreak}
procedure RepSaveAs(FileName:String);
{保存为*.xls文件}
begin
try
ExlBook.saveas(FileName);
except
MessageDlg(‘不能访问文件,请关闭Microsoft Excel后再运行本程序!‘, mtError, [mbOk], 0);
end;
end;{RepSaveAs}
procedure RepPrivew(FileName:String);
{预览}
begin
RepCreat;
ExlApp.Visible :=True;
try
ExlApp.workBooks.Open(FileName);
ExlApp.Workbooks[1].WorkSheets[1].PrintPreview;
finally
ExlApp.Quit;
ExlApp:=Unassigned;
//ExlApp:=‘‘;
end;{try}
end;{RepPrivew}
procedure RepQuit;
{退出Excel}
begin
ExlBook.Close;
ExlApp.Quit; //退出Excel Application
ExlApp:=Unassigned; //释放VARIANT变量
end;{RepQuit}
procedure RepDestroy;
{非正常退出Excel}
begin
if Not VarIsEmpty(ExlApp) then
RepQuit;
end;{RepDestroy}
end.
[转]Delphi导出Excel的设置操作