Delphi控制Excel输出上标示例

2018-06-17 18:57:41来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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

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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:【心得】关于删除结构体动态数组指定项的正确方法

下一篇:delphi数组作为参数传值