Einzelnen Beitrag anzeigen

Blackpit

Registriert seit: 27. Feb 2019
77 Beiträge
 
#60

AW: csv Datei Import ClassHelper für TClientDataSet

  Alt 24. Mai 2019, 16:33
...
Falls ihr es nicht gemerkt habt. Das ist ein Code-Bibliothek Beitrag. Hier sollte nicht so viel diskutiert werden.
...
Sehe ich auch so, deshalb hier noch ein verbesserter Vorschlag unter Berücksichtigung von Schokohase und Uwes Empfehlung Odd vs. mod 2
Delphi-Quellcode:
procedure TClientDataSetCsvClassHelper.LoadFromFile;
var
  myString :Variant;
  slFile :TStringList;
  slRow :TStringList;
  x,i,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 else
           begin
           cntFields := 0;
           myString := slFile[myPos];
           if Odd(length( myString )-
                length( stringreplace( myString, QuoteChar,'',[rfreplaceall, rfIgnoreCase]))) then
             while Odd(length( myString )-
                length( stringreplace( myString, QuoteChar,'',[rfreplaceall, rfIgnoreCase]))) do
             begin
             Inc(mypos);
             myString := myString+slFile[myPos];
             cntFields := length( myString )-
                length( stringreplace( myString, Delimiter,'',[rfreplaceall, rfIgnoreCase]));
             end
           else myFields := slRow.Count;
          if cntFields > 0 then myFields := cntFields + 1;
          FieldDefs.Clear;
          for i := 0 to myFields - 1 do
            begin
              FieldDefs.Add( Format( 'Field%d',[i]), ftWideString, StringLength );
            end;
          end;
          if FirstLineTitle then
          begin
          for i := 0 to myFields - 1 do
              FieldDefs.Add( slRow[i], ftWideString, StringLength );
          end;
          CreateDataSet;
          Active := True;
          cntFields := 0;
        end;
        begin
        if (x=0) and FirstLineTitle then inc( myPos );
        if myPos = slFile.Count then break;
        If myString = 'then myString := slFile[myPos];
        cntFields := length( myString )-
              length( stringreplace( myString, Delimiter,'',[rfreplaceall, rfIgnoreCase]));
          while cntFields <= (myFields-1) do
          if FirstLineTitle and (cntFields < (myFields-1)) then
            begin
              inc( myPos );
              myString := myString+slFile[myPos];
              cntFields := length( myString )-
                length( stringreplace( myString, Delimiter,'',[rfreplaceall, rfIgnoreCase]));
            end
            else
            if not FirstLineTitle and (x <> 0) then
            begin
            If myString = 'then myString := slFile[myPos];
            while Odd(length( myString )-
              length( stringreplace( myString, QuoteChar,'',[rfreplaceall, rfIgnoreCase]))) do
             if Odd(length( myString )-
                (length( stringreplace( myString, QuoteChar,'',[rfreplaceall, rfIgnoreCase])))) then
                begin
                inc( myPos );
                myString := myString+slFile[myPos];
                cntFields := length( myString )-
                  length( stringreplace( myString, Delimiter,'',[rfreplaceall, rfIgnoreCase]));
                end
             else
                cntFields := length( myString )-
                  length( stringreplace( myString, Delimiter,'',[rfreplaceall, rfIgnoreCase]));
            end
            else
            break;
        end;
          // fill TClientDataset
        DisableControls;
        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;
HTH
  Mit Zitat antworten Zitat