delphi7中DBGrid如何读取txt文件 比如将文件a.txt导入后 效果如下图
a文本里的内容是a4d9a2p08d|xfu8g184a6owk7r2rn|uiw7g382a9ddyk4jwz0r|gmyg2492a9fsg0uf2k1h|wol85...
a文本里的内容是
a4d9a2p08d|xfu8g184
a6owk7r2rn|uiw7g382
a9ddyk4jwz0r|gmyg2492
a9fsg0uf2k1h|wol8545
abs23766312|uks8g374
acegyckvjl|gpn2r556
ademfgbr|yyy9f9r91
aegxjxhct|vmw84r88
aeh69515578|fsir1732
ahs691737|yum9r74r9
ahzufl78501|uhz7r298
aie8ax6mu5|yjzr9r398
ajonm9nfig|kxa4907
akwcwphy0w|seqr7166
aoktqkpar|qmv64r81
aqz2477670|ognr5252 展开
a4d9a2p08d|xfu8g184
a6owk7r2rn|uiw7g382
a9ddyk4jwz0r|gmyg2492
a9fsg0uf2k1h|wol8545
abs23766312|uks8g374
acegyckvjl|gpn2r556
ademfgbr|yyy9f9r91
aegxjxhct|vmw84r88
aeh69515578|fsir1732
ahs691737|yum9r74r9
ahzufl78501|uhz7r298
aie8ax6mu5|yjzr9r398
ajonm9nfig|kxa4907
akwcwphy0w|seqr7166
aoktqkpar|qmv64r81
aqz2477670|ognr5252 展开
展开全部
uses
SysUtils, Dialogs, DB, DBGrids, Classes, Forms;
var
i:integer;
str,s帐号,s密码:string;
sList:TStringList;
// Form1:TForm; //窗体
cds:TClientDataSet; //添加在界面的控件
DataSource1:TDataSource;
DBGrid1:TDBGrid;
begin // main
sList := TStringList.Create;
try
sList.LoadFromFile('E:\test.txt');
except
showmessage('文件不存在');
Exit;
end;
if sList.Count = 0 then
Exit;
cds := TClientDataSet.Create(nil);
cds.FieldDefs.Add('序号',ftInteger,0,true);
cds.FieldDefs.Add('帐号',ftString,100,true); //长度为100
cds.FieldDefs.Add('密码',ftString,100,false); //同上
cds.CreateDataSet;
cds.Open;
for i := 0 to sList.Count - 1 do
begin
str := trim(sList.Get(i)); // name|password
if pos('|',str) = 0 then
continue; // 异常数据
s帐号 := copy(str,1,pos('|',str)-1); // name
s密码 := copy(str,pos('|',str)+1,length(str)-pos('|',str)); // pawwsord
// s密码 := copy(str,pos('|',str)+1,length(str)); // 也可
{
if length(s帐号) > 100 then
s := copy(s帐号,1,100);
if length(s密码) > 100 then
s := copy(s密码,1,100);
}
cds.Append;
cds.FieldByName('序号').AsInteger := i+1;
cds.FieldByName('帐号').AsString := s帐号;
cds.FieldByName('密码').AsString := s密码;
cds.Post;
end;
DataSource1.DataSet := cds;
DBGrid1.DataSource := DataSource1;
sList.Free;
showmessage('test...');
end.
SysUtils, Dialogs, DB, DBGrids, Classes, Forms;
var
i:integer;
str,s帐号,s密码:string;
sList:TStringList;
// Form1:TForm; //窗体
cds:TClientDataSet; //添加在界面的控件
DataSource1:TDataSource;
DBGrid1:TDBGrid;
begin // main
sList := TStringList.Create;
try
sList.LoadFromFile('E:\test.txt');
except
showmessage('文件不存在');
Exit;
end;
if sList.Count = 0 then
Exit;
cds := TClientDataSet.Create(nil);
cds.FieldDefs.Add('序号',ftInteger,0,true);
cds.FieldDefs.Add('帐号',ftString,100,true); //长度为100
cds.FieldDefs.Add('密码',ftString,100,false); //同上
cds.CreateDataSet;
cds.Open;
for i := 0 to sList.Count - 1 do
begin
str := trim(sList.Get(i)); // name|password
if pos('|',str) = 0 then
continue; // 异常数据
s帐号 := copy(str,1,pos('|',str)-1); // name
s密码 := copy(str,pos('|',str)+1,length(str)-pos('|',str)); // pawwsord
// s密码 := copy(str,pos('|',str)+1,length(str)); // 也可
{
if length(s帐号) > 100 then
s := copy(s帐号,1,100);
if length(s密码) > 100 then
s := copy(s密码,1,100);
}
cds.Append;
cds.FieldByName('序号').AsInteger := i+1;
cds.FieldByName('帐号').AsString := s帐号;
cds.FieldByName('密码').AsString := s密码;
cds.Post;
end;
DataSource1.DataSet := cds;
DBGrid1.DataSource := DataSource1;
sList.Free;
showmessage('test...');
end.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询