web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How to export to CSV file in UTF-8 without BOM

(0) ShareShare
ReportReport
Posted on by

Dear all,

I try to export multi records to a CSV file in UTF-8 without BOM.

i used this code:

System.Text.Encoding encoding = new System.Text.UTF8Encoding(false);
;
System.IO.File::WriteAllText(@'C:\test.txt', "Data 123", encoding);

Is this right? And How can i export multi records with break line?

Please give me a advice. Thank you so much.

*This post is locked for comments

I have the same question (0)
  • Mea_ Profile Picture
    60,284 on at
    RE: How to export to CSV file in UTF-8 without BOM

    Hi Hiep Nguyen,

    You can use standard AX class commaTextIo to create CSV files, please refer to this blog post fro example

    http://daxdilip.blogspot.co.nz/2011/08/commatextio-class-how-to-export-data-to.html

    As you can see commaTextIo.write() is called in a loop to insert multiple records.

    To set encoding you can use 3 parameter of commaTextIo constructor

    commaTextIo = new CommaTextIo(fileitemdata,#io_write, 65001);

    where 65001 is UTF-8

  • Community Member Profile Picture
    on at
    RE: How to export to CSV file in UTF-8 without BOM

    Thank you.

    But it's only utf-8. I want to export CSV utf8 without BOM.

  • Verified answer
    Mea_ Profile Picture
    60,284 on at
    RE: How to export to CSV file in UTF-8 without BOM

    AX cannot create file without BOM, however, using you code you can resave it.

        #File
        CommaTextIo      commaTextIo;
        FileIOPermission permission;
        CustTable        custTable;
        str              fileName = @"c:\test.csv";
        System.Text.Encoding encoding = new System.Text.UTF8Encoding(false);
        
        permission = new FileIOPermission(fileName,#io_write);
        permission.assert();
        commaTextIo = new CommaTextIo(fileName,#io_write, 65001);
    while select custTable { commaTextIo.write(custTable.AccountNum); } CodeAccessPermission::revertAssert(); commaTextIo = null; //save CSV without BOM new InteropPermission(InteropKind::ClrInterop).assert(); System.IO.File::WriteAllText(filename, System.IO.File::ReadAllText(filename), encoding); CodeAccessPermission::revertAssert();
  • Community Member Profile Picture
    on at
    RE: How to export to CSV file in UTF-8 without BOM

    Dear Ievgen Miroshnikov,

    Thank you so much. I try your code and it run perfectly. 

  • Community Member Profile Picture
    on at
    RE: How to export to CSV file in UTF-8 without BOM

    Did anyone got this working in batch mode for AX 2012. I get below error in batch mode

    “Can’t rewrite C:\Testing FOlder\Studio_Matrix.csv file without BOM”

  • Martin Dráb Profile Picture
    236,879 Most Valuable Professional on at
    RE: How to export to CSV file in UTF-8 without BOM

    Do you mean that WriteAllText() throws an exception? If so, what's the type and message? Isn't the file already open?

  • Community Member Profile Picture
    on at
    RE: How to export to CSV file in UTF-8 without BOM

    The line "System.IO.File::WriteAllText(filename, System.IO.File::ReadAllText(filename), encoding);" doesn't seem to work in batch it throws error In batch resulting in the file is being saved with BOM. On client the WriteAll method rewrites the file without BOM.

    Error message: “Can’t rewrite <C:\Testing FOlder\Studio_Matrix.csv> file without BOM”

     

    I am I closing the file before it is rewritten


  • Suggested answer
    Community Member Profile Picture
    on at
    RE: How to export to CSV file in UTF-8 without BOM

    Solution for this would be directly write the UTF-8 without BOM file using code where lobValue is a string data instead of trying to rewrite the file to convert to a file without BOM

    System.IO.File::WriteAllText(textIoPathFileName,lobValue , encoding);


     

  • Suggested answer
    sivakarthk Profile Picture
    10 on at
    RE: How to export to CSV file in UTF-8 without BOM

    Great!, solved.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Tocauer Profile Picture

Martin Tocauer 4

#2
Alexey Lekanov Profile Picture

Alexey Lekanov 2

#2
NNaumenko Profile Picture

NNaumenko 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans