Delphi 控制Excel 網路上有很多範例
所以這裡只列出一些最基礎的 及 網路上找不到較偏門的操作
基礎
uses System.Win.ComObj;
{$REGION '常數 一堆很難記的數字'}
const e_alright=-4152;
e_alleft=-4131;
e_alcenter=-4108;
e_albottom=-4107;
e_altop=-4160;
e_noboard=-4142;// xlLineStyleNone 無邊框
{$ENDREGION}
var
ExcelApp: Variant;
//以下都是使用 ExcelApp 此變數
//Excel初始化
{$REGION 'excel 前置動作'}
ExcelApp := CreateOleObject('Excel.Application');
ExcelApp.Visible := false;
ExcelApp.Caption := 'Application use Microsoft Excel';
ExcelApp.WorkBooks.Add;
ExcelApp.WorkBooks.Open(SFileName);
//ExcelApp.DisplayAlerts := False; //把警告訊息關掉
{$ENDREGION}
//基本填值
ExcelApp.Cells[r,c].Value:=CValue;
ExcelApp.Cells[r,c].Font.Name := 'Arial';
ExcelApp.Cells[r,c].Font.Size := 8;
{$REGION '合併儲存格 輸入參數範例 A1 A6'}
procedure ExcelMerge(StartCol,EndCol:string;Align:integer); overload;
begin
if Align<>0 then
begin
ExcelApp.ActiveSheet.Range[StartCol,EndCol].HorizontalAlignment := Align;
ExcelApp.ActiveSheet.Range[StartCol,EndCol].VerticalAlignment := Align;
end;
ExcelApp.ActiveSheet.Range[StartCol+':'+EndCol].Merge;
end;
{$ENDREGION}
ExcelApp.cells[1,1].Select; //不下這行 他捲軸會自動捲到右邊?
ExcelApp.cells[1,1].NumberFormatLocal:='@'; //設為文字格式
ExcelApp.ActiveWorkBook.SaveAs(Filename+'buf'); //存檔
ExcelApp.ActiveWorkBook.Saved := True; //要save 不然會跳視窗
ExcelApp.ActiveWorkBook.SaveAs(Filename,56); //Format 97-2003 強制存為 xls檔
.Font.Bold := True; //粗體
//邊框 1234 左右上下
.Borders[4].Weight := w; //邊框
.Interior.ColorIndex := 43 ; //底色
關於顏色 只能使用56種 見http://blog.udn.com/WayCheng/2705003
ExcelApp.ActiveSheet.Rows[NRow] //整行選取
//釋放Excel變數
ExcelApp.WorkBooks.Close;
ExcelApp.Quit;
VarClear(ExcelApp);
ExcelApp:=unassigned;
//以下是較特別的使用方式
//縮放到一頁寬高
ExcelApp.ActiveSheet.PageSetup.Zoom := false;
ExcelApp.ActiveSheet.PageSetup.FitToPagesWide := 1;
ExcelApp.ActiveSheet.PageSetup.FitToPagesTall := 1;
ExcelApp.ActiveSheet.UsedRange.Copy; //复制所有
ExcelApp.ActiveSheet.PasteSpecial; //这个是粘贴,PasteSpecial有红色报错,可以运行的。
//不顯示格線 (就是Excel > 檢視 > 格線 > 取消打勾)
ExcelApp.ActiveWindow.DisplayGridlines := False;
ExcelApp.Cells[r,c].WrapText:=True; //自動符合大小 斷行
//關閉相容性檢查
ExcelApp.ActiveWorkBook.CheckCompatibility := False;
若遇到 #N/A 這種Excel公式錯誤 會直接跳紅叉
須提前判斷公式錯誤
if VarIsError(ExcelApp.Cells[INRow,ExcelCol].Value) then
直接讀取該單元格公式
ExcelApp.Cells[1,15].Formula
自動符合欄寬
ExcelApp.columns.autofit;
Excel 分頁範例
const
sss: array[0..3] of string=('aa','bb','ccc','dd');
var
i: integer;
ExcelApp: Variant;
begin
try
ExcelApp := CreateOleObject('Excel.Application');
ExcelApp.Visible := false;
ExcelApp.Caption := 'Application use Microsoft Excel';
ExcelApp.WorkBooks.Add;
//ExcelApp.SheetsInNewWorkbook[1] := Length(sss);
//ExcelApp.WorkSheets[1].delete;
for i := 0 to Length(sss)-1 do begin
ExcelApp.WorkSheets.Add;
ExcelApp.WorkSheets[1].Activate;
ExcelApp.ActiveSheet.Name := sss[i];
ExcelApp.ActiveSheet.Cells[Random(5)+1,Random(5)+1].Value := sss[i];
end;
ExcelApp.WorkSheets[ExcelApp.WorkSheets.count].delete; //砍掉第一張分頁
DeleteFile('C:\1.xlsx');
ExcelApp.ActiveWorkBook.SaveAs('C:\1.xlsx'); //存檔
ExcelApp.ActiveWorkBook.Saved := True; //要save 不然會跳視窗
finally
ExcelApp.WorkBooks.Close;
ExcelApp.Quit;
VarClear(ExcelApp);
ExcelApp:=unassigned;
end;
速查表...
一堆VBA可以用的 在Delphi 得查這個表
https://msdn.microsoft.com/en-us/library/office/aa221100(v=office.11).aspx

大大, 不知您是否願意DELPHI教學(付費)呢???
您好 不知道我有甚麼可以幫你的呢? 如果問題需要詳談 可以留聯絡方式到我的信箱 heart75047@gmail .com