我做过的一段代码:
var
Excel: variant;
l,r: integer;
WorkBook, Sheet: Variant;
begin
try
Excel:=UnAssigned;
Excel:=CreateOleObject('Excel.Application');
Excel.Visible := True;
except
Excel:=UnAssigned;
raise Exception.Create('无法启动 Excel');
end;
try
//Excel.SheetsInNewWorkbook := 1;
//Excel.WorkBooks.Add;
WorkBook := Excel.WorkBooks.Add;
Sheet := WorkBook.WorkSheets[1];
if chkGrid.Checked then //--输出数据
for r := 0 to Grid1.RowCount-1 do
for l := 0 to Grid1.ColCount-1 do
Sheet.Cells[r+1, l+1] := Grid1.Cells[l,r];
if chkChart.Checked then //--输出数据
begin
Chart1.CopyToClipboardBitmap;
Excel.ActiveSheet.PasteSpecial;
end;
finally
Excel:=Unassigned;
WorkBook:=Unassigned;
Sheet:=Unassigned;
end;
end;