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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Creating CSV file through code using streamwriter

(0) ShareShare
ReportReport
Posted on by 302

Hello All,

I have created a logic to create a CSV file through code using streamwriter by addling values to the list and using strList2str(List, ',') I have used ',' as the delimiter. I am fetching records form the table and creating this CSV file and it is working fine except for few fields because of the field value(for eg: Address field - california, USA). This field is getting split because it has a , value.

Is there any workaround to achieve this?

Thanks

I have the same question (0)
  • Suggested answer
    André Arnaud de Calavon Profile Picture
    300,911 Super User 2025 Season 2 on at

    Hi Wekey,

    Are you able to use another delimiter or quotes around the Address field? With quotes it should know the comma is part of the field value instead of a separator.

  • Blue Wang Profile Picture
    on at

    Hi Wekey,

    I agree. You can try to use  other delimiter , provide it as a parameter.

    Such as strList2Str(list, ';').

    You can find existing examples:

    83060.PNG

  • Suggested answer
    Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    strList2str() won't indeed give you valid X , because it doesn't add quotation marks around strings such as addresses.

    I would use CommaTextStreamIo. For example:

    CommaTextStreamIo io = CommaTextStreamIo::constructForWrite();
    io.writeExp(['Joe', 'California, USA']);
    File::SendFileToUser(io.getStream(), 'file.csv');

  • Wekey Profile Picture
    302 on at

    Hello Andre,

    Thanks for your reply.

    I am using List(string) and also adding values using list.addend() so by default the values are between quotes.

    for eg:

    value 1:"california, USA"

    Value 2:"someValue"

    Regarding deLimiter I am using ',' because default delimiter for csv is ','.

    Please suggest if there is anything I can change here.

    Thanks

  • André Arnaud de Calavon Profile Picture
    300,911 Super User 2025 Season 2 on at

    HI Wekey,

    In my region, the default CSV separator is commonly a semicolon. Also a pipe is used a lot. This all prevents issues with a comma in the text.

  • Wekey Profile Picture
    302 on at

    Thanks all for your reply.

    But If I use a different delimiter or use commaIo class my file is getting created in a different way,

    pastedimage1590398149977v1.png

    So when I use this file for DMF import its throwing an error. If I use ',' delimiter file looks like this,

    pastedimage1590398300013v2.png

    Thanks

  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    CSV is a text format. Your images don't show CSV; they show how Microsoft Excel interprets your CSV texts.

    If the default interpretation doesn't work for you (as in this case), use Data > From Text and select properties as needed.

  • Wekey Profile Picture
    302 on at

    Hello Martin,

    The problem is I am creating the csv file through code and using the file for DMF process in the code itself. I am trying something like this,

    community.dynamics.com/.../msdyn365fo-execution-of-data-projects-through-x-code

    Is there any suggestion for this?

    Thanks

  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    Suggestion for what? You showed us a screenshot of an Excel spreadsheet, but now you seem to be saying that you problem isn't about Excel at all. The link is about importing data from code, so it sounds like you have some problem with it - but you didn't tell us what the problem is.

    Also, what's the point of doing that? You're running X++ code to export data to a file, import this file again, get the original data and write it into a data entity. Why you don't you skip all the stuff about files and don't call the data entity directly?

  • Wekey Profile Picture
    302 on at

    I am importing values from external sql DB. I am fetching records from external DB and creating a CSV file and creating a DMF project with that CSV this is what exactly I am trying.

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans