频道分类

delphi 数字转Excel字母列序号

作者:admin 来源: 日期:2021/2/10 20:49:05 人气: 标签:

 
unit Unit3;

interface

uses
  Math, Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls, Vcl.Samples.Spin, Vcl.ExtCtrls;

type
  TForm3 = class(TForm)
    Memo1: TMemo;
    Panel1: TPanel;
    Button1: TButton;
    SpinEdit1: TSpinEdit;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation



{$R *.dfm}

function ExcelNumToLetter(AExcelNum: Integer): string;
var
  vLen: integer;
  vNum: Integer;
begin
  vLen := Ord('Z') - Ord('A') + 1;
  Result := '';
  vNum := AExcelNum;
  while (vNum >= 0) do
  begin
    Result := Char(vNum mod vLen + Ord('A')) + Result;
    vNum := Math.floor(vNum / vLen) - 1;
  end;
end;

procedure TForm3.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Add(Format('数字【%d】对应的Excel字母列号为【%s】', [SpinEdit1.Value, ExcelNumToLetter(SpinEdit1.Value)]));
end;

procedure TForm3.Button2Click(Sender: TObject);
var
  i: Integer;
begin
  Memo1.Clear;
  Memo1.Lines.BeginUpdate;
  for i := 0 to 300 do
  begin
    Memo1.Lines.Add(Format('数字【%d】对应的Excel字母列号为【%s】', [i, ExcelNumToLetter(i)]));
  end;
  Memo1.Lines.EndUpdate;
end;

procedure TForm3.FormCreate(Sender: TObject);
begin
  Memo1.Clear;
end;

end.

//数字转Excel字母列序号 byQQ16643506

上一篇:delphi 检测CPU内核数下一篇:没有资料