Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

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

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

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

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

    Great!, solved.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee 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);


     

  • Community Member Profile Picture
    Community Member Microsoft Employee 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


  • Martin Dráb Profile Picture
    Martin Dráb 230,445 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
    Community Member Microsoft Employee 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”

  • Community Member Profile Picture
    Community Member Microsoft Employee 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. 

  • Verified answer
    Mea_ Profile Picture
    Mea_ 60,278 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
    Community Member Microsoft Employee 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.

  • Mea_ Profile Picture
    Mea_ 60,278 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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans