Page 1 of 1

ExportCSV Unicode

Posted: Fri Mar 18, 2011 2:27 pm
by PStadler
Hello,

I have an XLS-File with Unicode. I want to export it to UTF-8 or Unicode. How can this be achieved?

Sincerely

Peter

Re: ExportCSV Unicode

Posted: Wed Mar 23, 2011 6:55 pm
by larsa
Hello

Right now you can't, but you can easily fix this by doing some changes to the file ExportCSV4.pas. Use the (delphi) class TEncoder to encode the file in UTF-8.
Don't forget that you may also have to write a BOM (Byte Order Mark) at the beginning of the file.

Re: ExportCSV Unicode

Posted: Mon Mar 28, 2011 3:17 pm
by PStadler
Hello,

I am trying do use an Embarcadero example.

implementation


var
LBuffer: TBytes;
LByteOrderMark: TBytes;
LOffset: Integer;
utf8Text: UTF8String;
L: Cardinal;
LEncoding, DestEncoding: TEncoding;
FileText: string;
.......

begin
Stream := TFileStream.Create(Filename,fmCreate);
try
Sheet := XLS.Sheet[SheetIndex];
Sheet.CalcDimensions;
if ecoSelection in Options then begin
for i := 0 to Sheet.SelectedAreas.Count - 1 do
ExportRange(Sheet.SelectedAreas[i].Col1,Sheet.SelectedAreas[i].Row1,Sheet.SelectedAreas[i].Col2,Sheet.SelectedAreas[i].Row2);
end
else
begin
DestEncoding := TEncoding.Unicode;
LBuffer := LEncoding.Convert(LEncoding, DestEncoding, LBuffer,
LOffset, Length(LBuffer) - LOffset);
LOffset := TEncoding.GetBufferEncoding(LBuffer, DestEncoding);
FileText := DestEncoding.GetString(LBuffer, LOffset, Length(LBuffer) - LOffset);
L := Length(FileText);
SetLength(utf8Text, L * SizeOf(Char) + 1);
L := UnicodeToUtf8(PAnsiChar(utf8Text), Length(utf8Text), PWideChar(FileText), L);
// New destination encoding
DestEncoding := TEncoding.UTF8;
try
LByteOrderMark := DestEncoding.GetPreamble;
Stream.Write(LByteOrderMark[0], Length(LByteOrderMark));
Stream.Write(utf8Text[1], L);
finally
//Stream.Free;
end;
end;
finally
Stream.Free;
end;
end;

In the ExportRange function

LBuffer := LBuffer +S2;

will not work, because of compilation error.

Can you give an working example?

Sincerely

Peter

Re: ExportCSV Unicode

Posted: Thu Mar 31, 2011 11:53 am
by PStadler
Hello,

I managed with help of Remy Lebeau from TeamB to export the tab delimited File to any Encoding available.

https://forums.codegear.com/thread.jspa ... 6&tstart=0

In Procedure ExportRange I use a TStringList to write out string s. Then in

TStringList.List.SaveToFile(f.FileName,TEncoding.Unicode) any Encoding, also TEncoding.UTF8
can be applied.

Sincerely

Peter