我的一段 delphi 控制 Excel 的代码

需要把数据导出到 Excel 中
学会录制宏,帮助很大

{  保存成 excel  }
procedure TFormPayBank.FSaveExcel(ds:TDataSet;haveWidth:boolean);
var
  ExcelApp: Variant;
  r,c:integer;    // row column
  rc:integer;
begin
  //r := 1;
  //c := 1;
  screen.Cursor := crHourGlass;
  self.Caption := '正在导出数据......';
  ExcelApp := CreateOleObject( 'Excel.Application');
  try
    ExcelApp.Caption := '银行报盘';
    ExcelApp.WorkBooks.Add;
    ExcelApp.WorkBooks[1].Activate;
    ds.First;
    rc := ds.RecordCount;
    for r := 1 to rc do
    begin
      for c := 1 to ds.FieldCount-1 do
      begin
        ExcelApp.Cells[r,c] := ds.Fields[c-1].AsString
      end;
      ds.Next;
    end;
    if haveWidth then
    begin
      ExcelApp.ActiveSheet.Columns[1].ColumnWidth := 19;
      ExcelApp.ActiveSheet.Columns[1].NumberFormatLocal := '@';
      //ExcelApp.ActiveSheet.Range('''A1:A'+intTostr(rc)+'''').HorizontalAlignment:='xlLeft';
      ExcelApp.ActiveSheet.Columns[2].ColumnWidth := 16;
      ExcelApp.ActiveSheet.Columns[3].ColumnWidth := 42;
    end;
    //ExcelApp.SaveAs('c:/a.xls');
    ExcelApp.Visible := true;
  finally
    //ExcelApp.WorkBooks.Close;
    ExcelApp := null;
    screen.Cursor := crDefault;
    self.Caption := '银行报盘';
  end;

end; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值