直接上代碼吧,這個示例在Excel中輸出一個M2:
unit FfrmMain; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, RzButton; type TfrmMain = class(TForm) RzBitBtn1: TRzBitBtn; procedure RzBitBtn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var frmMain: TfrmMain; const FileName: string = 'd:\t.xls'; implementation {$R *.dfm} Uses ComObj; procedure TfrmMain.RzBitBtn1Click(Sender: TObject); var XlApp, XlWorkbook, XlSheet: Olevariant; i, j: integer; Range: Olevariant; begin Try XlApp := createOleObject('Excel.Application'); XlApp.visible := false; XlWorkbook := XlApp.workbooks.add; XlSheet := XlWorkbook.sheets.add; except showmessage('你還沒有安裝Microsoft Excel,請先安裝!'); XlApp.Quit; XlSheet := Unassigned; XlWorkbook := Unassigned; XlApp := Unassigned; exit; end; XlSheet.Cells[1, 1] := 'M2'; XlSheet.Cells[1, 1].Characters[2, 1].Font.Superscript := True; XlSheet.rows[1].insert; XlSheet.Columns.EntireColumn.AutoFit; if FileExists(FileName) then begin if MessageDlg('文件已經存在,要替換嗎?', mtConfirmation, [mbyes, mbno], 0) = mrno then begin XlApp.Quit; XlSheet := Unassigned; XlWorkbook := Unassigned; XlApp := Unassigned; exit; end; end; try XlWorkbook.SaveAs(FileName); except showmessage('導出失敗,請檢查你對該文件是否有寫權限!'); XlApp.Quit; XlSheet := Unassigned; XlWorkbook := Unassigned; XlApp := Unassigned; exit; end; showmessage('導出成功!'); XlWorkbook.Saved := True; XlApp.Quit; XlSheet := Unassigned; XlWorkbook := Unassigned; XlApp := Unassigned; end; end.
關鍵是:
XlSheet.Cells[1, 1] := 'M2'; XlSheet.Cells[1, 1].Characters[2, 1].Font.Superscript := True;
至於屬性,查資料或通過宏來看吧。