呵呵,需要有固定格式的txt文件吧!
解决方案 »
- 改变AdvStringGrid中数据的显示格式。
- 刚学接口,有点问题不明.
- 散分,凡是认识我的干妹妹都来领分
- 郁闷啊郁闷!怎么我就净遇上些莫名其妙的问题!在这里提问,就从来没人给过正确答案!
- 为什么在win7下,Image1不能透明?
- 怎样打印dbgrid中的内容(delphi)? 大侠请帮忙,小妹先行谢过了!
- fastreport的打印次数
- Delphi 怎么获取窗口或其他控件的滚动条的句柄啊?
- 如何卸载Direct X
- 高分,解决一个Class的问题
- oyo111111(卖火柴的),你在哪儿?清帮帮忙!
- 高分请教一个问题,如果只使用ADO+DBGrid,没有用TTable, TQuery, 还需要BDE的支持吗?
f:textfile;
a:array of char;
i:integer;
begin
AssignFile(F, 'a.txt');
Reset(F);
i:=0;
while not eof(f) do
begin
Readln(F,a[i]);
inc(i);
end;
CloseFile(F);
end;
procedure Tgetdataform.BtngetdataClick(Sender: TObject);
var
dataname,linedata,timeinfo,staidinfo,dayinfo,sqlcount,sqlinst,sqlupdt:string;
data: array of array of string;
status,datetime1,datetime2,datetime3,sqlonduty:string;
i,n:integer;
datafile:TextFile;
delta:boolean;
begin
inherited;
delta:=True;
i:=0;
If Opendialog1.Execute then
begin
dataname:=Opendialog1.FileName ;
End;
AssignFile(datafile,dataname);
If FileExists(dataname) then
begin
Reset(datafile);
while Not Eof(datafile) do
begin
Readln(datafile,linedata);
i:=i+1;
SetLength(data,i,4);
timeinfo:=copy(linedata,4,4);
timeinfo:= timeinfo+'/'+copy(linedata,8,2);
timeinfo:= timeinfo+'/'+copy(linedata,10,2);
timeinfo:= timeinfo+' '+copy(linedata,12,2);
timeinfo:= timeinfo+':'+copy(linedata,14,2);
status:=copy(linedata,17,1); dayinfo:= copy(linedata,4,4);
dayinfo:= dayinfo+'/'+copy(linedata,8,2);
dayinfo:= dayinfo+'/'+copy(linedata,10,2); staidinfo:=copy(linedata,19,length(linedata)-18);
data[i-1,0]:=timeinfo;
data[i-1,1]:=staidinfo;
data[i-1,2]:=dayinfo;
data[i-1,3]:=status; end;
CloseFile(datafile);
End;
dm.Database1.StartTransaction ;
try
//dm.query1.Database.
For n:=0 to i-1 do
begin
sqlcount:='select count(*) as c from tab_staff where sta_id= '''+data[n,1]+''' ';
try
dm.query2.close;
dm.query2.SQL.clear;
dm.query2.SQL.add(sqlcount);
dm.query2.open ;
Except
showmessage('數據庫錯誤!!');
end;
if dm.query2.FieldByName ('c').asinteger =0 then
delta:=False;
If delta then
begin
sqlcount:='select count(*) as c from tab_chkduty where sta_id= '''+data[n,1]+''' and chk_day between '''+data[n,2]+''' and '''+data[n,2]+''' ';
try
strtodatetime(data[n,2]);
except
showmessage('aa日期輸入無效');
end;
if data[n,2]<>formatdatetime('yyyy"/"mm"/"dd',strtodatetime(data[n,2])) then
begin
showmessage('bb考勤日期輸入無效!');
end; try
dm.query2.close;
dm.query2.SQL.clear;
dm.query2.SQL.add(sqlcount);
dm.query2.open ;
Except
showmessage('數據庫錯誤!!');
end;
if dm.query2.FieldByName ('c').asinteger =0 then
case StrToInt(data[n,3]) of
1:begin
//showmessage('這天這個工號的考勤記錄不存在,插入操作!');
datetime1:=formatdatetime('yyyy/MM/dd hh:mm',strtodatetime(data[n,0]));
datetime2:=formatdatetime('yyyy/MM/dd hh:mm',strtodatetime(data[n,2]+' 07:40'));
datetime3:=formatdatetime('yyyy/MM/dd hh:mm',strtodatetime(data[n,2]+' 07:46'));
if (StrToDateTime(datetime1)>StrToDateTime(datetime2)) and (StrToDateTime(datetime1)<StrToDateTime(datetime3)) then
data[n,0]:=datetime2;
sqlinst:='insert into tab_chkduty(sta_id,chk_day,chk_onduty,modtime) ';
sqlinst:=sqlinst+'values('''+data[n,1]+''','''+data[n,2]+''','''+data[n,0]+''','''+FormatDatetime('yyyymmdd hh:nn:ss',Date)+''' )';
try
dm.query1.close;
dm.query1.SQL.clear;
dm.query1.SQL.add(sqlinst);
dm.query1.execsql;
//showmessage('您已經成功添加了一條員工的考勤信息!');
Except
showmessage('2數據庫錯誤!!');
end;
end;
dm.Database1.commit;
showmessage('數據導入數據庫全部成功!!');
except
dm.Database1.Rollback;
showmessage('數據插入數據庫全部失敗!!');
end;end;
我觉得你可以使用TStrings类,这个类有LoadFromFile和SaveToFile方法
用这个方法从文件中读出数据后,再把TString.Items赋给数组即可。