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;
调用方法:
ExportStrGridToExcel([StringGrid1]);
ExportStrGridToExcel([StringGrid1, StringGrid2]);
技术支持:CoolSlob@163.com
导出StringGrid内容到Excel
ExportStrGridToExcel函数实现及调用
最新推荐文章于 2020-11-15 20:00:03 发布
博客展示了ExportStrGridToExcel函数的代码,该函数用于将字符串网格数据导出到Excel。代码中定义了相关变量,处理Excel应用程序的创建、工作簿和工作表的设置,将网格数据逐行逐列写入Excel。还给出了函数的调用方法,并提供了技术支持邮箱。
3373





