How to save an ANSI file from Business Central?
MortenSteengaard
Member Posts: 144
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
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
0
Best 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;
0
Answers
-
Hi @MortenSteengaard,
According to MS Docs the Windows format is ANSI :
https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-file-handling-and-text-encoding?wt.mc_id=DX-MVP-5003099#windows-formatFor help, do not use PM, use forum instead, perhaps other people have the same question, or better answers.0 -
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,
Morten0 -
So, how do you create the file? How do you tell the system to create the file with Windows encoding?0
-
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);0 -
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.0
-
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;
0 -
Hello,
My usual solution for many such problems, like encodings or for example Windows/Linux files and so on is the following:
- save the file to a given folder which is NOT the desired folder
- Use AI to generate a simple PowerShell script for the necessary conversions, adapt as necessary. After conversion it moves the file to the desired folder
- Schedule the PowerShell script to run every 5 minutes via Windows Task Scheduler. If more often say every 5 seconds is necessary, run it once and use a timed loop inside powershell.
- If you are worried the file is big and will be locked through writing, do this: once you have exported the file, also export an empty file with the same name and the extension .rdy and adapt the script to only process the file if it sees the .rdy file
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.7K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 324 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions

