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 tms web core 复制内容到剪切板

Delphi 实现一个程序在另一个程序内运行

Delphi实现网卡状态检测

Delphi源码获取网络图片缓存的地址

Delphi中几种程序自我删除的方法

Delphi 读写附加数据

Delphi 从listview组件中的选定行在数据库中删除

Delphi 判断一个颜色是否是亮色

dededl安卓中保持屏幕常亮

Delphi 打开网页的两种方法

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



打赏

取消

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

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

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

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

评论

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