Flexcel CSV Export newline and encoding

Hi we have been using the XLSX.Export function to create CSV files with a CRLF for newlines. As suggested in Setting CRLF vs LF

However we now encounter a problem when trying to combine this with exporting using UTF8 encoding. Is it possible to use both in the export function?

When looking at TExcelFile.Export Method | FlexCel Studio for VCL and FireMonkey documentation we don't see any possibilities covering our needs.

Seems we are able to the desired result.

              Writer := TStreamWriter.Create(MSReport, TEncoding.UTF8);
              Writer.BaseStream.Size := 0; // not UTF-8 BOM
              Try
                XLSX.Export(Writer, TXlsCellRange.Null, ';', true, #13#10);
              Finally
                Writer.Free;
              End

Indeed, the actual methods use a TextWriter, the ones that take a filename just create a textwriter and then call the methods that use TextWriter. If the combination you need is not available in the methods taking a filename, you need to use a TextWriter instead and just create it with the filename.

The BOM is controlled by the TEncoding you are using. If you use TEncoding.UTF8 indeed, that has a BOM. You need to define your own TEncoding without BOM, see utf 8 - Remove UTF-8 BOM from TStream output - Stack Overflow

But FlexCel already defines a class like this for some uses, so you can use it if you want.

To use it, you need to use code like this:

uses ..., __FlexCelTypes,..

  var UTF8Encoding := TUTF8EncodingNoBom.Create;
  try
    Writer := TStreamWriter.Create(MSReport, UTF8Encoding)
    ...
  finally
    UTF8Encoding.Free;
   end;
     

Sorry, I just remembered that you don't need to create an instance of TUTF8EncodingNoBom

You can simply use

  Writer := TStreamWriter.Create(MSReport, TUTF8EncodingNoBom.Instance)

No need to fee anything.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.