TXT格式为:
----------------------------------------
aaa bbb ccc dddd
bbaee aasdf xzcv adsf
df adf asdf (第二个数据没有)
dfbvbbb a32a ff (第四个数据没有)
----------------------------------------
注:中间中以TAB分隔,如果没有数据就为两个TAB
----------------------------------------
aaa bbb ccc dddd
bbaee aasdf xzcv adsf
df adf asdf (第二个数据没有)
dfbvbbb a32a ff (第四个数据没有)
----------------------------------------
注:中间中以TAB分隔,如果没有数据就为两个TAB
或者你可以用sql server的bcp或者dts直接就完成了阿,如果连续的tab会引起误差,你可以用stringreplace将tabtab用tabAtab代替,倒入后将值为A的update为null(你可以不用A,举例而已)
function Getchar(var str:string ):Tstrings;
var
num :Tstrings;
begin
str:=trim(str);
num:=tstringlist.Create;
while pos(' ',str)>0 do
begin
trim(str);
num.Add(copy(str,1,pos(' ',str)));
delete(str,1,pos(' ',str));
pos(' ',str);
end ;
num.Add(str);
Result:=num;
end;
我写过一个处理CSV文件的类,贴出来给大家看一看,请指教:
Unit CSVFileAccess;interfaceuses
SysUtils, Windows,Classes,ComCtrls;type
TCsvAccess=class(TObject)
private
CsvText:TStringList;
RowCount:Integer;
ColCount:Integer;
CsvArray:Array of Array of String;
public
constructor Create;overload;
destructor Destroy;override;
procedure AssignFile(FileName:String); //文件关联
procedure FormatAllValue; //文件"格式化",把文件内容存到一个二维数组内
function GetColCount:Integer; //得到列数
function GetRowCount:Integer; //得到行数
function GetHeaderAt(Index:Integer):String; //得到某列标题
function GetContentAt(Row,Col:Integer):String; //得到某行某列的值
end;implementationConstructor TCsvAccess.Create;
begin
CsvText:=TstringList.Create;
end;destructor TCsvAccess.Destroy;
begin
CsvText.Free;
end;procedure TCsvAccess.AssignFile(FileName:String);
begin
CsvText.LoadFromFile(FileName);
FormatAllValue;
end;procedure TCsvAccess.FormatAllValue;
var
Row,Col:Integer;
PosCount:Integer;
CsvStr:String;
begin
RowCount:=GetRowCount;
ColCount:=GetColCount;
SetLength(CsvArray,RowCount,ColCount);
for Row:=0 to RowCount-1 do
begin
CsvStr:=CsvText.Strings[Row];
for Col:=0 to ColCount-1 do
begin
if Pos(',',CsvStr)<>0 then
begin
PosCount:=Pos(',',CsvStr);
CsvArray[Row,Col]:=Copy(CsvStr,1,PosCount-1);
CsvStr:=Copy(CsvStr,PosCount+1,Length(CsvStr)-PosCount);
end
else
begin
CsvArray[Row,Col]:=CsvStr;
end;
end;
end;
end;function TCsvAccess.GetColCount:Integer;
var
Count,PosCount:Integer;
CsvStr:String;
begin
Count:=0;
CsvStr:=CsvText.Strings[0];
while Pos(',',CsvStr)<>0 do
begin
PosCount:=Pos(',',CsvStr);
Count:=Count+1;
CsvStr:=Copy(CsvStr,PosCount+1,Length(CsvStr)-PosCount);
end;
Count:=Count+1;
Result:=Count;
end;function TCsvAccess.GetRowCount:Integer;
begin
Result:=CsvText.Count;
end;function TCsvAccess.GetHeaderAt(Index:Integer):String;
begin
if Index<ColCount then
begin
Result:=CsvArray[0,Index];
end;
end;function TCsvAccess.GetContentAt(Row,Col:Integer):String;
begin
if (Row<RowCount) and (Col<ColCount) then
begin
Result:=CsvArray[Row,Col];
end;
end;end.
这样比较方便