Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2018-03

StringGrid1.SaveToCSVFile - UTF-8 with BOM

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mattias Gaertner
to Sandro Cumerlato
Hello, how can I save StringGrid content to a CSV file encoded to "UTF-8 with BOM" format? (as default it is saved to "UTF-8 without BOM" format)
var
  s: string;
  ms: TMemoryStream
ms:=TMemoryStream.Create;
try
  s:=UTF8BOM;
  ms.Write(s[1],length(s));
  Grid.SaveToCSVStream(ms)
  ms.Position:=0;
  ms.SaveToFile('foo.csv');
finally
  ms.Free;
end;

Reply by email

Michael Van Canneyt
to Mattias Gaertner
Mattias Gaertner wrote 21 lines
On Sat, 10 Mar 2018 16:57:08 +0100 Sandro Cumerlato via Lazarus wrote: > Hello, > how can I save StringGrid content to a CSV file encoded to "UTF-8 with BOM" > format? (as default it is saved to "UTF-8 without BOM" format) var s: string; ms: TMemoryStream ms:=TMemoryStream.Create; try s:=UTF8BOM; ms.Write(s[1],length(s)); Grid.SaveToCSVStream(ms) ms.Position:=0; ms.SaveToFile('foo.csv'); finally ms.Free; end;

You can also just use a filestream ?

Michael.

Reply by email

Sandro Cumerlato
to Michael Van Canneyt

Thank you Mattias and Michael for your help!

procedure TForm1.ButtonSaveClick(Sender: TObject);
const
  UTF8BOM = #$EF#$BB#$BF;
var
  s: String;
  fs: TFileStream;
begin
  s := ChangeFileExt(Application.ExeName, '.csv');
  try
    fs := TFileStream.Create(s, fmCreate);
    fs.Write(UTF8BOM, SizeOf(UTF8BOM));
    StringGrid1.SaveToCSVStream(fs, ';', True, False);
  finally
    fs.free;
  end;
end;

On 10 March 2018 at 17:25, Michael Van Canneyt via Lazarus <

Show quoted text 41 lines
wrote: On Sat, 10 Mar 2018, Mattias Gaertner via Lazarus wrote: On Sat, 10 Mar 2018 16:57:08 +0100 > Sandro Cumerlato via Lazarus wrote: > > Hello, >> how can I save StringGrid content to a CSV file encoded to "UTF-8 with >> BOM" >> format? (as default it is saved to "UTF-8 without BOM" format) >> > > var > s: string; > ms: TMemoryStream > > ms:=TMemoryStream.Create; > try > s:=UTF8BOM; > ms.Write(s[1],length(s)); > Grid.SaveToCSVStream(ms) > ms.Position:=0; > ms.SaveToFile('foo.csv'); > finally > ms.Free; > end; > You can also just use a filestream ? Michael. -- _______________________________________________ Lazarus mailing list https://lists.lazarus-ide.org/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20180311/eac63506/attachment-0001.html>

Reply by email