获取选中行中的数据提取并且保存到txt

function getchcount(query: TADOQuery): Integer;
var
i:integer;
begin
i:=0;
with Query do begin
Query.First;
while not Eof do begin
if Query.FieldByName('S_FLAG').AsBoolean then
i:=i+1;
next;
end;
end;
Result:=i; //返回行数,integer
end;

procedure TXJBoxForm.Button1Click(Sender: TObject);
var
cntr:string; //用于存放每一行的信息
i,j,s,checkcount:Integer;
list:TStringList; //用于存放整个箱号信息
begin
checkcount:=getchcount(MADOQuery); //选中总行数赋值
cntr:='';
j:=0; //循环个数,用户满16个换行
s:=0; //用于统计总循环行数
list:=TStringList.Create;
with MADOQuery do begin
MADOQuery.First;
while not Eof do begin
if MADOQuery.FieldByName('S_FLAG').AsBoolean then begin
if cntr='' then begin
cntr:=MADOQuery.FieldByName('BOX_NO').AsString;
j:=j+1;
s:=s+1;
end else begin
cntr:=cntr+','+MADOQuery.FieldByName('BOX_NO').AsString;
j:=j+1;
s:=s+1;
end;
if s=checkcount then begin //用于防止不是整行时剩余部门不会导出的情况
list.add(cntr); //添加到list中
cntr:=''; //清空,开始统计下一行
j:=0; //满16个换行,重置为0重新循环
end;
if (checkcount<17) and (j=checkcount) then begin
list.add(cntr);
cntr:='';
j:=0;
end else begin
if j=16 then begin
list.add(cntr);
j:=0;
cntr:='';
end;
end;
end;
Next;
end;
MADOQuery.First; //为保证在第一行数据开始循环
end;
//导出到txt功能
if SaveDialog1.Execute then //SaveDialog1 是一个TSaveDialog 组件
list.SaveToFile(SaveDialog1.FileName+'.txt');
showmessage('导出成功! 数量:'+inttostr(checkcount));

end;

原文地址:https://www.cnblogs.com/zyb2016/p/6092651.html