Einzelnen Beitrag anzeigen

Blup

Registriert seit: 7. Aug 2008
Ort: Brandenburg
1.464 Beiträge
 
Delphi 12 Athens
 
#8

AW: TStringlist wieder mit Trennzeichen speichern

  Alt 22. Jan 2020, 10:13
nicht getestet:
Delphi-Quellcode:
unit CSVHelper;

interface

uses
  Classes, SysUtils;

type
  TStringListCSVHelper = class helper for TStringList
  public
    procedure LoadFromCSVStream(Stream: TStream); overload; virtual;
    procedure LoadFromCSVStream(Stream: TStream; Encoding: TEncoding); overload; virtual;
    procedure LoadFromCSVFile(const FileName: string); overload; virtual;
    procedure LoadFromCSVFile(const FileName: string; Encoding: TEncoding); overload; virtual;
    procedure SaveToCSVStream(Stream: TStream); overload; virtual;
    procedure SaveToCSVStream(Stream: TStream; Encoding: TEncoding); overload; virtual;
    procedure SaveToCSVFile(const FileName: string); overload; virtual;
    procedure SaveToCSVFile(const FileName: string; Encoding: TEncoding); overload; virtual;
  end;

implementation

{TStringListCSVHelper]}

procedure TStringListCSVHelper.LoadFromCSVStream(Stream: TStream);
var
  Buffer: TBytes;
  Encoding: TEncoding;
begin
  Stream.ReadData(Buffer);
  TEncoding.GetBufferEncoding(Buffer, Encoding);
  DelimitedText := Encoding.GetString(Buffer);
end;

procedure TStringListCSVHelper.LoadFromCSVStream(Stream: TStream; Encoding: TEncoding);
var
  Buffer: TBytes;
begin
  Stream.ReadData(Buffer);
  DelimitedText := Encoding.GetString(Buffer);
end;

procedure TStringListCSVHelper.LoadFromCSVFile(const FileName: string);
var
  Stream: TStream;
begin
  Stream := TFileStream.Create(FileName);
  try
    LoadFromCSVStream(Stream);
  finally
    Stream.Free;
  end;
end;

procedure TStringListCSVHelper.LoadFromCSVFile(const FileName: string; Encoding: TEncoding);
var
  Stream: TStream;
begin
  Stream := TFileStream.Create(FileName, fmOpenRead);
  try
    LoadFromCSVStream(Stream, Encoding);
  finally
    Stream.Free;
  end;
end;

procedure TStringListCSVHelper.SaveToCSVStream(Stream: TStream);
begin
  SaveToCSVStream(Stream, Encoding);
end;

procedure TStringListCSVHelper.SaveToCSVStream(Stream: TStream; Encoding: TEncoding);
var
  Buffer: TBytes;
begin
  Buffer := Encoding.GetBytes(DelimitedText);
  Stream.WriteData(Buffer);
end;

procedure TStringListCSVHelper.SaveToCSVFile(const FileName: string);
var
  Stream: TStream;
begin
  Stream := TFileStream.Create(FileName, fmOpenWrite);
  try
    SaveToCSVStream(Stream);
  finally
    Stream.Free;
  end;
end;

procedure TStringListCSVHelper.SaveToCSVFile(const FileName: string; Encoding: TEncoding);
var
  Stream: TStream;
begin
  Stream := TFileStream.Create(FileName, fmOpenWrite);
  try
    SaveToCSVStream(Stream, Encoding);
  finally
    Stream.Free;
  end;
end;

end.
Delphi-Quellcode:
uses
  CSVHelper;
  
function BuildFilename(DateTime: TDateTime): string;
begin
  Result := 'BDECN ' + FormatDateTime('yyyy_mm_dd__hh_mm_ss', DateTime) + '.dat';
end;

procedure SaveData(MyCSV: TStringList);
begin
  MyCSV.Delimiter := ';';
  MyCSV.StrictDelimiter := True;
  MyCSV.SaveToCSVFile(BuildFilename(Now));
end;

procedure LoadData(MyCSV: TStringList);
begin
  MyCSV.Delimiter := ';';
  MyCSV.StrictDelimiter := True;
  MyCSV.LoadFromCSVFile(BuildFilename(Now));
end;

Geändert von Blup (22. Jan 2020 um 10:17 Uhr)
  Mit Zitat antworten Zitat