给你几个函数,不要使用SaveToFile。
将数据集保存为CSV文件格式,Memo类字段中的回车符被替换为‘#’,不支持其他Blob字段。
也可从CSV文件导入。
CSV文件可以使用Excel编辑、处理。
unit Share;
interface
uses
Sysutils,IniFiles,classes,ShellApi,FileCtrl,DBTables,Db;
procedure ExportDataSetToCSVFile(const aDataSet:TDataSet;
const aFileName:string);
procedure ImportFromCSVFile(const AFileName:String;
const ADataSet:TDataSet);
function GetLeftWord(var ASentence:string; ADelimiter:char):string;
function GetLeftWordCSV(var ASentence:string):string;
implementation
function GetLeftWordCSV(var ASentence:string):string;
begin
Result:='';
ASentence:=Trim(ASentence);
if Length(ASentence)=0 then exit;
if ASentence[1]='"' then begin
Delete(ASentence,1,1);
Result:=GetLeftWord(ASentence,'"');
GetLeftWord(ASentence,',');
end else
Result:=GetLeftWord(ASentence,',');
end;
function GetLeftWord(var ASentence:string; ADelimiter:char):string;
var i:integer;
begin
Result := '';
i := Pos(ADelimiter,ASentence);
if i = 0 then begin
Result := Trim(ASentence);
ASentence := '';
end else begin
Result:=trim(Copy(ASentence,1,i-1));
Delete(ASentence,1,i);
end;
end;
procedure ExportDataSetToCSVFile(const aDataSet:TDataSet;
const aFileName:string);
var aTextFile:TextFile;
i:integer;
aValue,LineStr:string;
DataSetActive:boolean;
begin
DataSetActive := aDataSet.Active;
if not aDataSet.Active then aDataSet.Open;
aDataSet.DisableControls;
try
aDataSet.FieldDefs.Update;
if aDataSet.RecordCount=0 then exit;
try
AssignFile(aTextFile,aFileName);
Rewrite(aTextFile);
LineStr := '';
for i:=0 to aDataSet.FieldCount-1 do begin
aValue := aDataSet.Fields.DisplayName ;
if Pos(',',aValue) > 0 then aValue:='"' + aValue + '"';
LineStr := LineStr + aValue + ',';
end;
Delete(LineStr,length(LineStr),1);// remove extra ','
Writeln(aTextFile,LineStr);
aDataSet.First;
while not aDataSet.EOF do begin
LineStr := '';
for i := 0 to aDataSet.FieldCount-1 do begin
if aDataSet.Fields.IsBlob then begin
if aDataSet.Fields.DataType=ftMemo then
aValue :=StringReplace(aDataSet.Fields.AsString,#13+#10,'#',[rfReplaceAll]);
end else begin
aValue := aDataSet.Fields.AsString;
end;
if Pos(',',aValue) > 0 then aValue := '"' + aValue + '"';
LineStr := LineStr+aValue+',';
end;
Delete(LineStr,length(LineStr),1);// remove extra ','
Writeln(aTextFile,LineStr);
aDataSet.Next;
end;// while
finally
try
Flush(aTextFile);
CloseFile(aTextFile);
except
end;
end;
finally
aDataSet.EnableControls;
aDataset.Active := DataSetActive;
end;
end;
procedure ImportFromCSVFile(const AFileName:String;
const ADataSet:TDataSet);
const cMaxFields=1023;
var
aTextFile:TextFile;
i,j,HeaderCount:integer;
FldPtr:array[0..cMaxFields] of integer;
aColValue,aLineString,aValue:string;
HasCommonField,DataSetActive:boolean;
begin
if not FileExists(AFileName) then exit;
AssignFile(aTextFile,AFileName);
Reset(aTextFile);
try
Readln(aTextFile, ALineString);
HasCommonField:=False;
HeaderCount:=0;
DataSetActive:=ADataSet.Active;
if not ADataset.Active then ADataset.Open;
ADataSet.FieldDefs.Update;
while (length(ALineString) > 0) and (HeaderCount < cMaxFields) do begin
aColValue := GetLeftWordCSV(ALineString);
for i:=0 to ADataSet.FieldCount-1 do
if ADataSet.Fields.DisplayName=aColValue then
begin
j :=i;
FldPtr[HeaderCount] := j;
if j > -1 then HasCommonField:=True;
inc(HeaderCount);
break;
end;
end;
if not HasCommonField then exit;
ADataSet.DisableControls;
try
while not EOF(aTextFile) do begin
Readln(aTextFile,ALineString);
j := -1;
ADataSet.Append;
while (length(ALineString) > 0) and (j < HeaderCount-1) do begin
aColValue := GetLeftWordCSV(ALineString);
inc(j);
if FldPtr[j] = -1 then continue; // skip unmatched columns
try
if ADataSet.Fields[FldPtr[j]].DataType=ftMemo then
ADataSet.Fields[FldPtr[j]].AsString :=StringReplace(aColValue,'#',#13+#10,[rfReplaceAll])
else
ADataSet.Fields[FldPtr[j]].AsString := aColValue;
except
//DoSomeThing
end;
end;
ADataSet.Post;
end;
finally
ADataSet.EnableControls;
ADataSet.Active := DataSetActive;
end;
finally
CloseFile(aTextFile);
end;
end;
end.