[轉]Delphi匯出Excel的設定作業

來源:互聯網
上載者:User

標籤:

//設定頁面邊界:

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的設定作業

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.