var
ExcelApp, Sheet: Variant;
begin
try
ExcelApp := CreateOleObject('Excel.Application');
except
raise Exception.Create('没有发现Excel,请先确认是否安装Excel.');
Exit;
end;
try
ExcelApp.WorkBooks.Add(xlWorkSheet);
ExcelApp.WorkBooks[1].WorkSheets[1].Name := '数据列表';
Sheet := ExcelApp.WorkBooks[1].WorkSheets[1];
with Table do
begin
for I := 0 to FieldCount - 1 do
ExcelApp.WorkBooks[1].Sheets[1].Cells[1, I + 1] := Fields.DisplayLabel;
J := 1;
First;
while not Eof do
begin
J := J + 1;
for I := 0 to FieldCount - 1 do
ExcelApp.WorkBooks[1].Sheets[1].Cells[J, I + 1] := Fields.AsString;
Application.ProcessMessages;
if cbxDelete.Checked then
Delete //如果勾了删除选项,就将这条数据删除
else
Next; //否则就指向下一条记录
end;
end;
Sheet.SaveAs(SaveDialog.FileName);
finally
Sheet := Unassigned;
ExcelApp.WorkBooks[1].Close;
end;
end;
这个程序是将数据从数据库中导入到Excel中,跟你的要求差不多。