Registriert seit: 27. Feb 2019
77 Beiträge
|
AW: csv Datei Import ClassHelper für TClientDataSet
16. Mai 2019, 21:09
Hi Folks,
hier eine Implemtierung für LF in Quoted-Fields:
Code:
// v1.1 - 26.02.2019 - Blackpit: Anpassung LF in Quotes
/// /////////////////////////////////////////////////////////////////////////////
procedure TClientDataSetCsvClassHelper.LoadFromFile;
var
myString :Variant;
slFile :TStringList;
slRow :TStringList;
x :Integer;
i :Integer;
j :Integer;
myPos :Integer;
myFields :Integer;
cntFields :Integer;
begin
slFile := TStringList.Create;
slRow := TStringList.Create;
try
myPos := 0;
slRow.Delimiter := Delimiter;
slRow.QuoteChar := QuoteChar;
slRow.StrictDelimiter := True;
slFile.StrictDelimiter := True;
slFile.LoadFromFile( Filename );
// ClientDataset Initialisieren
if slFile.Count > 0 then
begin
Active := False;
for x := 0 to slFile.Count - 1 do
begin
myString := '';
if (x = 0) then
begin
myPos := x;
// Build Header for DS
slRow.DelimitedText := slFile[0];
if FirstLineTitle then myFields := slRow.Count;
FieldDefs.Clear;
for i := 0 to myFields - 1 do
begin
if FirstLineTitle and ( slRow[i] <> '' ) then
begin
FieldDefs.Add( slRow[i], ftWideString, StringLength );
end
else
begin
FieldDefs.Add( Format( 'Field%d',[i]), ftWideString, StringLength );
end;
end;
CreateDataSet;
Active := True;
myString := slFile[myPos];
end;
begin
if (x=0) and FirstLineTitle then inc( myPos );
if myPos = slFile.Count then
break;
myString := slFile[myPos];
cntFields := length( myString )-
length( stringreplace( myString, Delimiter,'',[rfreplaceall, rfIgnoreCase]));
while cntFields < (myFields-1) do
begin
inc( myPos );
myString := myString+slFile[myPos];
cntFields := length( myString )-
length( stringreplace( myString, Delimiter,'',[rfreplaceall, rfIgnoreCase]));
end;
end;
// fill TClientDataset
DisableControls;
if (myPos > 0) then
begin
slRow.DelimitedText := myString;
Append;
for j := 0 to slRow.Count - 1 do
begin
Fields[j].AsString := slRow[j];
end;
Post;
end;
EnableControls;
inc( myPos );
end;
end;
finally
slFile.Free;
slRow.Free;
end;
end;
Über Feedback würde ich mich freuen.
HTH somebody
Geändert von Blackpit (21. Mai 2019 um 16:42 Uhr)
Grund: Korrektur ohne Header 1.Satz
|
|
Zitat
|