ExportCSV Unicode

Questions and answers on how to use XLSReadWriteII 3/4/5.
Post Reply
PStadler
Posts: 44
Joined: Sat Feb 18, 2006 8:19 am

ExportCSV Unicode

Post 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
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: ExportCSV Unicode

Post 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.
Lars Arvidsson, Axolot Data
PStadler
Posts: 44
Joined: Sat Feb 18, 2006 8:19 am

Re: ExportCSV Unicode

Post 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
PStadler
Posts: 44
Joined: Sat Feb 18, 2006 8:19 am

Re: ExportCSV Unicode

Post 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
Post Reply