文本文件和数据集相互转换
{*******************************************************} { } { 陈新光 } { } { 版权所有 (C) 2008 } { } {*******************************************************} //将以TAB作分隔符的文本文件写入数据表中 //当然如果是以逗号作分隔符的也可以
unit uImpTxt;
interface
uses SysUtils,db,Classes;
{------------------------------------------------------------------------------- 过程名: 分割字符串 作者: 陈新光 日期: 2008.06.24 参数: Source(源字符串), Deli(分割标志): string 返回值: TStringList -------------------------------------------------------------------------------} function SplitString(Source, Deli: string ): TStringList; {------------------------------------------------------------------------------- 过程名: 将固定格式的文本文件写入数据集中 作者: 陈新光 日期: 2008.06.24 参数: ADataSet:TDataSet;ATxtFile,ASplit(分隔符):string 返回值: Boolean -------------------------------------------------------------------------------} function TxtToDataset(ADataSet:TDataSet;ATxtFile,ASplit:string):Boolean; {------------------------------------------------------------------------------- 过程名: 将数据集导出成文本文件 作者: 陈新光 日期: 2008.06.24 参数: ADataSet:TDataSet;ATxtFile,ASplit(分隔符):string 返回值: Boolean -------------------------------------------------------------------------------} function DatasetToTxt(ADataSet:TDataSet;ATxtFile,ASplit:string):Boolean;
implementation
function SplitString(Source, Deli: string ): TStringList; var EndOfCurrentString: byte; StringList:TStringList; begin StringList:=TStringList.Create; while Pos(Deli, Source)>0 do begin EndOfCurrentString := Pos(Deli, Source); StringList.add(Copy(Source, 1, EndOfCurrentString - 1)); Source := Copy(Source, EndOfCurrentString + length(Deli), length(Source) - EndOfCurrentString); end; Result := StringList; StringList.Add(source); end;
function TxtToDataset(ADataSet:TDataSet;ATxtFile,ASplit:string):Boolean; var i,j:Integer; ss,mx: TStringList; line:string; begin Result:=False; if (not ADataSet.Active) or (not FileExists(ATxtFile)) then Exit; ss:=TStringList.Create; try ss.LoadFromFile(ATxtFile); //将文本文件装入TSTRINGLIST for i:=0 to ss.Count-1 do //遍历TSTRINGLIST begin line:=ss.Strings[i]; //取当前行字符串 mx:=SplitString(line,ASplit); ADataSet.DisableControls; ADataSet.Append; for j:=0 to ADataSet.FieldCount-1 do //给对应的字段赋值 ADataSet.Fields[j].AsString:=mx.Strings[j]; ADataSet.Post; end; if Assigned(mx) then mx.Free; finally ss.free; end; ADataSet.EnableControls; Result:=True; end;
function DatasetToTxt(ADataSet:TDataSet;ATxtFile,ASplit:string):Boolean; var ss:TStringList; i:Integer; line:string; begin Result:=False; if (not ADataSet.Active) or (ASplit='') then Exit; line:=''; //初始化变量 ss:=TStringList.Create; try ADataSet.DisableControls; ADataSet.First; while not ADataSet.Eof do begin for i:=0 to ADataSet.FieldCount-1 do begin if i=0 then line:=line+ADataSet.Fields[i].AsString //不要分隔符 else line:=line+ASplit+ADataSet.Fields[i].AsString; //要分隔符 end; ss.Add(line); line:=''; ADataSet.Next; end; ss.SaveToFile(ATxtFile); finally ss.Free; end; ADataSet.EnableControls; Result:=True; end;
end.
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/archive/2008/06/24/2940861.html