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 android windows ios通用获取程序版本

Delphi 按esc快捷键退出程序的简单方法

Delphi 的内存操作函数(1): 给字符指针分配内存

Delphi 在桌面上画图

Delphi 安卓app的http访问权限

Delphi 获取其它软件的tlistbox句柄,怎么取得listbox中的数据

Delphi 外壳扩展创建快捷方式和获取快捷方式的目标对象

Delphi 程序启动窗体控制在桌面右下角

Delphi利用系统环境变量获取常用系统目录

Delphi批量删除同类文件(带通配符)

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



打赏

取消

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

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

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

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

评论

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