delphi 如何将stringgrid中数据导出到excel中?
我有一个stringgrid里面有2个字段的数据,通过按扭把数据导出到excel中?代码怎么写??谢谢~...
我有一个stringgrid 里面有2个字段的数据,通过按扭把数据导出到excel中?代码怎么写??
谢谢~ 展开
谢谢~ 展开
展开全部
delphi(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;
调用方法:
ExportStrGridToExcel([StringGrid1]);
ExportStrGridToExcel([StringGrid1, StringGrid2]);
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]);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询