张志峰的博客

水滴石川,积少成多。

导航

Delphi TXLSReadWriteII2 导出Excel

Posted on 2012-06-13 09:02  ╰★张志峰★╮  阅读(956)  评论(0编辑  收藏  举报

 

TXLSReadWriteII2版本导出Excel文件:

 

procedure TForm1.N1Click(Sender: TObject);

var

  i: Integer;

  aSaveDialog: TSaveDialog;

  aFileName, aStampTime: AnsiString;

  aXlsObj: TXLSReadWriteII2;

  p: PDataRec;

begin

  aSaveDialog := TSaveDialog.Create(Self);

  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

    aSaveDialog.Free;

  end;

 

  aXlsObj := TXLSReadWriteII2.Create(nil);

  try

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

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

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

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

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

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

 

    for i:=1 to FDataHash.Count - 1 do

    begin

      p := FDataHash[i];

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

      aXlsObj.Sheets[0].AsWideString[1, i] := p.table;

      aXlsObj.Sheets[0].AsWideString[2, i] := p.kind;

      aXlsObj.Sheets[0].AsWideString[3, i] := Format('%d', [p.rows]);

      aXlsObj.Sheets[0].AsWideString[4, i] := Format('%d', [p.times]);

      aXlsObj.Sheets[0].AsWideString[5, i] := p.desc;

    end;

 

    aXlsObj.Filename := aFileName;

    aXlsObj.Write;

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

  finally

    aXlsObj.Free;

  end;

end;