How to save an ANSI file from Business Central?

Hi experts,

This is regarding Business Central cloud.

When I make a file, I can set the encoding to Windows, UTF-8, UTF-16 and MSDOS.

Our customer need a file with ANSI encoding.

How do I do that?

Hope, you can help.

Morten

Best Answer

  • MortenSteengaardMortenSteengaard Member Posts: 140
    Answer ✓
    Hi SanderDk and vaprog,

    Thank you for your reply.

    Since Microsoft don't support making an ANSI encoded file (with our local, Danish, letters), I have made it myself (with a little help from my friend, Erik Hougaard on YouTube).

    This way it is working fine and our customer does not have to pay for the Azure functions.

    I saw how Erik Hougaard made an app that created a WAVE file and I used that to make the code below. I know it does not support all letters and signs, but this is what the customer is willing to pay for - and it is all they need.

    procedure SaveAnsiEncodedTextFileFromTempBlob(var TempBlobLocal: Codeunit "Temp Blob"; FileName: Text)
    var
        InStream, IStream : InStream;
        OutStream: OutStream;
        TempBlobLocal2: Codeunit "Temp Blob";
        TextToWrite: Text;
        i: Integer;
        Byte1, Byte2, Byte3, Byte4, Byte5, Byte6 : Byte;
    begin
        // Read text from TempBlob
        TempBlobLocal.CreateInStream(InStream, TextEncoding::UTF8);
        InStream.ReadText(TextToWrite);

        TempBlobLocal2.CreateOutStream(OutStream);

        Byte1 := 230; // æ
        Byte2 := 248; // ø
        Byte3 := 229; // å
        Byte4 := 198; // Æ
        Byte5 := 216; // Ø
        Byte6 := 197; // Å

        for i := 1 to StrLen(TextToWrite) do begin
            if TextToWrite = 'æ' then
                OutStream.Write(Byte1)
            else if TextToWrite = 'ø' then
                OutStream.Write(Byte2)
            else if TextToWrite = 'å' then
                OutStream.Write(Byte3)
            else if TextToWrite = 'Æ' then
                OutStream.Write(Byte4)
            else if TextToWrite = 'Ø' then
                OutStream.Write(Byte5)
            else if TextToWrite = 'Å' then
                OutStream.Write(Byte6)
            else
                OutStream.Write(TextToWrite);
        end;

        // Save the file
        TempBlobLocal2.CreateInStream(IStream);

        DownloadFromStream(IStream, '', '', '', FileName);
    end;

Answers

  • SanderDkSanderDk Member Posts: 504
    For help, do not use PM, use forum instead, perhaps other people have the same question, or better answers.
  • MortenSteengaardMortenSteengaard Member Posts: 140
    Hi SanderDk,
    Thank you for your reply.
    You are right, but when I save the file on my local computer, open it in Notepad and select "Save as", then I can see that it is "UTF-8".
    The file must be imported into "EG Lønservice" and the letters æ, ø and å are not correct after the import.
    If I manually make a text file with ANSI encoding using Notepad, then it works fine.
    Best regards,
    Morten
  • vaprogvaprog Member Posts: 1,156
    So, how do you create the file? How do you tell the system to create the file with Windows encoding?
  • MortenSteengaardMortenSteengaard Member Posts: 140
    Hi vaprog,
    I have the data in a Temp Blob and save it to a file like this:

    TempBlob.CreateInStream(IStream, TEXTENCODING::Windows);
    //TempBlob.CreateInStream(IStream, TEXTENCODING::UTF8);
    //TempBlob.CreateInStream(IStream, TEXTENCODING::MSDos);

    Filename := 'Test.txt';
    DownloadFromStream(IStream, '', '', '', Filename);
  • vaprogvaprog Member Posts: 1,156
    I would expect what you do to work correctly. If it does not, I'd write to a tempfile, then download that tempfile. This way you can set the TEXTENCODING on the outstream.
  • MortenSteengaardMortenSteengaard Member Posts: 140
    Answer ✓
    Hi SanderDk and vaprog,

    Thank you for your reply.

    Since Microsoft don't support making an ANSI encoded file (with our local, Danish, letters), I have made it myself (with a little help from my friend, Erik Hougaard on YouTube).

    This way it is working fine and our customer does not have to pay for the Azure functions.

    I saw how Erik Hougaard made an app that created a WAVE file and I used that to make the code below. I know it does not support all letters and signs, but this is what the customer is willing to pay for - and it is all they need.

    procedure SaveAnsiEncodedTextFileFromTempBlob(var TempBlobLocal: Codeunit "Temp Blob"; FileName: Text)
    var
        InStream, IStream : InStream;
        OutStream: OutStream;
        TempBlobLocal2: Codeunit "Temp Blob";
        TextToWrite: Text;
        i: Integer;
        Byte1, Byte2, Byte3, Byte4, Byte5, Byte6 : Byte;
    begin
        // Read text from TempBlob
        TempBlobLocal.CreateInStream(InStream, TextEncoding::UTF8);
        InStream.ReadText(TextToWrite);

        TempBlobLocal2.CreateOutStream(OutStream);

        Byte1 := 230; // æ
        Byte2 := 248; // ø
        Byte3 := 229; // å
        Byte4 := 198; // Æ
        Byte5 := 216; // Ø
        Byte6 := 197; // Å

        for i := 1 to StrLen(TextToWrite) do begin
            if TextToWrite = 'æ' then
                OutStream.Write(Byte1)
            else if TextToWrite = 'ø' then
                OutStream.Write(Byte2)
            else if TextToWrite = 'å' then
                OutStream.Write(Byte3)
            else if TextToWrite = 'Æ' then
                OutStream.Write(Byte4)
            else if TextToWrite = 'Ø' then
                OutStream.Write(Byte5)
            else if TextToWrite = 'Å' then
                OutStream.Write(Byte6)
            else
                OutStream.Write(TextToWrite);
        end;

        // Save the file
        TempBlobLocal2.CreateInStream(IStream);

        DownloadFromStream(IStream, '', '', '', FileName);
    end;
Sign In or Register to comment.