TXLSReadWriteII 公式计算

时间:2022-07-30 15:40:44
unit Unit1;

interface

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, XLSReadWriteII2, StdCtrls, XLSUtils2;

type

  TForm1 = class(TForm)

    aXlsObj: TXLSReadWriteII2;

    Button1: TButton;

    aSaveDialog: TSaveDialog;

    procedure Button1Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

var

  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

var

  i, j: Integer;

  aFileName, aStampTime: AnsiString;

  aa: TFormulaValue;

begin

  try

    aSaveDialog.InitialDir := ExtractFilePath(ParamStr(0));

    aSaveDialog.DefaultExt := 'xls';

    aSaveDialog.Filter := 'Excel文件(*.xls)|*.xls';

    aStampTime := FormatDateTime('yyyymmddhhnnss', Now);

    aSaveDialog.FileName := aStampTime;

    if not aSaveDialog.Execute then

      Exit;

    aFileName := aSaveDialog.FileName;

    if aFileName = '' then

      Exit;

  finally

  end;

  try

    aXlsObj.Sheets[0].AsWideString[0, 0] := 'id';

    aXlsObj.Sheets[0].AsWideString[1, 0] := 'formula';

    aXlsObj.Sheets[0].AsWideString[2, 0] := 'lie1';

    aXlsObj.Sheets[0].AsWideString[3, 0] := 'lie2';

    for i:=1 to 10 do

    begin

      aXlsObj.Sheets[0].AsWideString[0, i] := Format('%d', [i+1]);

      aXlsObj.Sheets[0].AsFormula[1, i] := Format('C%d + D%d', [i+1, i+1]);

      aXlsObj.Sheets[0].AsFloat[2, i] := ( i * 5);

      aXlsObj.Sheets[0].AsFloat[3, i] := ( i * 4);

    end;

    for i:=1 to 10 do

    begin

      aa := aXlsObj.Sheets[0].AsFormulaValue[1 ,i];

      case aa.ValType of

        fvFloat:   ShowMessage(FloatToStr(aa.vFloat));

      end;

    end;

    aXlsObj.Filename := aFileName;

    aXlsObj.Write;

    ShowMessage(Format('导出文件'+#13#10+'%s'+#13#10 +'成功!', [aFileName]));

  finally

  end;

end;

end.