delphi中TStringGrid数据的导出

没什么好说的,就是一个导出Excel的功能,代码如下:

function TExportToExcel.Export(SGrid_List: TStringGrid;filename:string;out Errstr:string): Boolean;
var
  excelapp:Variant;
  page:Variant;
  i,j:Integer;
  savedialog:TSaveDialog;
  strsavefile:string;
begin
  try
    result := false;
    savedialog := TSaveDialog.Create(self);
    try
      savedialog.FileName := filename;
      savedialog.Filter := '*.xls';
      if not savedialog.Execute() then
        exit;
      strsavefile := savedialog.FileName;
      Screen.Cursor:=crhourglass; //屏幕指针形状
      excelapp := CreateOleObject('excel.application');
      excelapp.workbooks.add;
      excelapp.workbooks[1].worksheets[1].name:=filename;
      page:=excelapp.workbooks[1].worksheets[filename];
      for i:= 0 to SGrid_List.RowCount-2 do
      begin
        //第一列没有数据,不用
        for j:=1 to SGrid_List.ColCount-1 do
        begin
          page.cells[i+1,j]:= SGrid_List.Cells[j,i];
        end;
      end;
      excelapp.activeworkbook.saveas(strsavefile);
      Application.ProcessMessages;
      excelapp.application.quit;
      result := true;
    finally
      savedialog.Free;
      Screen.Cursor:=crDefault;
    end;
  except
    on E:Exception do
    begin
      Errstr := e.Message;
      result := false;
    end;
  end;
end;

 

转载于:https://www.cnblogs.com/jinshizuofei/p/3373150.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值