直接上代码吧,这个示例在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;

至于属性,查资料或通过宏来看吧。

posted on 2016-12-26 15:09  garfieldtom  阅读(1104)  评论(2编辑  收藏  举报