delphi2010:把stringgrid数据保存到EXCEL


本文整理自网络,侵删。

 函数定义如下:

function ExportStrGridToExcel(Args: array of const): Boolean;
var
  iCount, jCount: Integer;
  XLApp: Variant;
  Sheet: Variant;
  I: Integer;
begin
  Result := False;
  if not VarIsEmpty(XLApp) then
  begin
    XLApp.DisplayAlerts := False;
    XLApp.Quit;
    VarClear(XLApp);
  end;

  try
    XLApp := CreateOleObject('Excel.Application');
  except
    Exit;
  end;

  XLApp.WorkBooks.Add;
  XLApp.SheetsInNewWorkbook := High(Args) + 1;

  for I := Low(Args) to High(Args) do
  begin
    with TStringGrid(Args[I].VObject) do
    begin
      XLApp.WorkBooks[1].WorkSheets[I+1].Name := Name;
      Sheet := XLApp.Workbooks[1].WorkSheets[Name];

      for jCount := 0 to RowCount - 1 do
      begin
        for iCount := 0 to ColCount - 1 do
        begin
          Sheet.Cells[jCount + 1, iCount + 1] := Cells[iCount, jCount];
        end;
      end;
    end;
  end;
  XlApp.Visible := True;
end;

调用方法如下:

procedure TForm1.Button6Click(Sender: TObject);
begin
ExportStrGridToExcel([StringGrid1]);

end;

相关阅读 >>

Delphi 检查ip地址合法性

Delphi xe10 文件目录/路径操作 (andorid、ios、windows)

Delphi xe 的 tdictionary

Delphi tchart 的使用

Delphi textfile读取文本文件

Delphi superobject json操作类的基本用法

Delphi禁止用鼠标拖动窗口的大小

Delphi winapi: writeprivateprofilestring、getprivateprofilestring - 简单读写 ini 文件

Delphi webservices base64编码

Delphixe7关于android 检测屏幕是否处于关闭状态

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...