Excel是很多人喜歡使用的文書處理工具。學會Delphi對Excel的操控方法,讓系統在做資料的匯入、匯出更為得心應手。
👇選取儲存格
ExcelApp.ActiveSheet.Cells.EntireColumn //所有欄
ExcelApp.ActiveSheet.Cells.EntireRow //所有列
ExcelApp.ActiveSheet.Cells //所有儲存格
ExcelApp.ActiveSheet.Columns[1] //第一欄
ExcelApp.ActiveSheet.Rows[1] //第一列
ExcelApp.ActiveSheet.Cells[r,c] //第r列第c欄
ExcelApp.ActiveSheet.Range[起,迄] //區間選擇(起訖可以是欄、列、格)
👇設定儲存格
ExcelApp.ActiveSheet.Columns[1].ColumnsWidth := 5; //設置第一欄欄寬爲5(5字符個數寬)
ExcelApp.ActiveSheet.Rows[2].RowHeight := 16 //設置第二列列高爲16
ExcelApp.ActiveSheet.Rows[1].NumberFormatLocal:='@';//將儲存格改成文字格式
ExcelApp.Selection.NumberFormatLocal:='@'; //將儲存格改成文字格式
ExcelApp.ActiveSheet.Range['A1:C3'].Merge; //合併儲存格
ExcelApp.ActiveSheet.Cells.EntireColumn.AutoFit; //最適欄寬
ExcelApp.ActiveSheet.Cells.EntireRow.AutoFit; //最適列高
ExcelApp.ActiveSheet.Rows[1].HorizontalAlignment := -4108;//水平置中
//(靠左:-4131;靠右:-4152)
ExcelApp.ActiveSheet.Rows[1].VerticalAlignment := -4108; //垂直置中
ExcelApp.ActiveSheet.Rows[1].WrapText := True; //自動換列
ExcelApp.ActiveSheet.Columns[1].Hidden := True; //隱藏
ExcelApp.ActiveSheet.Columns[1].NumberFormatLocal := '0.00%'; //設定欄位格式[百分比]
ExcelApp.ActiveSheet.Cells[1,1].Interior.ColorIndex := 38; //設定底色為玫瑰色
👇設定邊框線
ExcelApp.ActiveSheet.Range[ 'B3:D4' ].Borders[2].Weight := 3; //指定右邊框線寬度3
.Borders[2] //1-左 2-右 3-頂 4-底 5-斜( \ ) 6-斜( / )
.LineStyle := 1; //指定邊框線的style 1:實線, 2:虛線
.Weight := 3; //指定邊框線的 Weight (1~4)
.ColorIndex := 1; //指定邊框的 ColorIndex (0~56)
//1:黑色 2:白色 3:紅色 4:綠色 5:藍色 6:黃色 7:紫色 8:青藍色 9:棕色..
👇填值
ExcelApp.ActiveSheet.Cells[2,4].Value := '這是第二行第四列'; //填值
ExcelApp.ActiveSheet.Cells[1,1].Formula:= '公式'; //填入公式
ExcelApp.ActiveSheet.Cells[1,4].ClearContents; //清除公式
ExcelApp.ActiveSheet.Cells[1,1].HasFormula //儲存格是否有公式
👇字體屬性
ExcelApp.ActiveSheet.Rows[1].Font.Name := '隸書';
ExcelApp.ActiveSheet.Rows[1].Font.Color := clBlue;
ExcelApp.ActiveSheet.Rows[1].Font.Bold := True;
ExcelApp.ActiveSheet.Rows[1].Font.UnderLine := True;
ExcelApp.ActiveSheet.Rows[1].Font.Size:= 12;
👇版面設定
ExcelApp.ActiveSheet.PageSetup.PrintTitleRows := '$1:$1'; //列印標題列
ExcelApp.ActiveSheet.PageSetup.CenterHeader := '表頭'; //中頁首
ExcelApp.ActiveSheet.PageSetup.LeftHeader := '頁次: &P / &N'; //左頁首
ExcelApp.ActiveSheet.PageSetup.RightHeader := '製表人'; //右頁首
ExcelApp.ActiveSheet.PageSetup.CenterFooter := '& &P / &N'; //中頁尾
ExcelApp.ActiveSheet.PageSetup.LeftFooter := '頁次: &P / &N'; //左頁尾
ExcelApp.ActiveSheet.PageSetup.RightFooter := '製表人'; //右頁尾
xcelApp.ActiveSheet.PageSetup.PrintArea := '$B$1:$N$150'; //設定列印範圍
ExcelApp.ActiveSheet.PageSetup.Orientation := 2; //1.直印 2.橫印
ExcelApp.ActiveSheet.PageSetup.Zoom := 65; //列印時縮小成65%
ExcelApp.ActiveSheet.PageSetup.Zoom := True; //使用頁次縮放功能
ExcelApp.ActiveSheet.PageSetup.FitToPagesWide := 1; //縮放成一頁寬(需配合Zoom=True)
ExcelApp.ActiveSheet.PageSetup.FitToPagesTall := 1; //縮放成一頁高(需配合Zoom=True)
ExcelApp.ActiveSheet.PageSetup.PaperSize := 8; //設定紙張大小 8:A3、9:A4
ExcelApp.ActiveSheet.PageSetup.HeaderMargin := 2/0.035; //頁首頂邊距
ExcelApp.ActiveSheet.PageSetup.FooterMargin := 3/0.035; //頁尾底邊距
ExcelApp.ActiveSheet.PageSetup.TopMargin := 2/0.035; //頂邊距2cm
ExcelApp.ActiveSheet.PageSetup.BottomMargin := 2/0.035; //底邊距2cm
ExcelApp.ActiveSheet.PageSetup.LeftMargin := 2/0.035; //左邊距2cm
ExcelApp.ActiveSheet.PageSetup.RightMargin := 2/0.035; //右邊距2cm
ExcelApp.ActiveSheet.PageSetup.CenterHorizontally := True; //頁面水平居中
ExcelApp.ActiveSheet.PageSetup.CenterVertically := True; //頁面垂直居中
ExcelApp.ActiveSheet.PageSetup.PrintGridLines := True; //格網線
👇凍結窗格
ExcelApp.ActiveSheet.Cells[3,3].Select;
ExcelApp.ActiveWindow.FreezePanes := True;
👇拷貝操作
ExcelApp.ActiveSheet.Used.Range.Copy; //拷貝整個工作表
ExcelApp.ActiveSheet.Range[ 'A1:E2' ].Copy; //拷貝指定區域
ExcelApp.ActiveSheet.Range.[ 'A1' ].PasteSpecial; //從A1位置開始粘貼
ExcelApp.ActiveSheet.Range.PasteSpecial; //從文件尾部開始粘貼
ExcelApp.ActiveSheet.Rows[2].Insert; //插入一列
ExcelApp.ActiveSheet.Columns[1].Insert; //插入一欄
ExcelApp.ActiveSheet.Rows[2].Delete; //刪除一列
ExcelApp.ActiveSheet.Columns[1].Delete; //刪除一欄