本文整理自网络,侵删。
函数定义如下:
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 获取其它软件的tlistbox句柄,怎么取得listbox中的数据
更多相关阅读请进入《Delphi》频道 >>