• Delphi控制Excel输出上标示例


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

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

  • 相关阅读:
    梯度下降算法 (转)
    机器学习
    杭电1097-A hard puzzle
    动态规划初步习题(紫书)
    4.21考试题解
    【bzoj4445 scoi2015】小凸想跑步
    【bzoj4444 scoi2015】国旗计划
    【bzoj4443 scoi2015】小凸玩矩阵
    【luogu P4007 清华集训2017】小Y和恐怖奴隶主
    【luoguP4006 清华集训2017】小Y和二叉树
  • 原文地址:https://www.cnblogs.com/GarfieldTom/p/6222383.html
Copyright © 2020-2023  润新知