Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Answered

Text Formatting of Excel cells to export data through code

Posted on by 45

Hi All,

Is it possible to format an excel cell to 'Text' before exporting data to excel by x++ code?
For example, a String field value stored in AX as 000011 should be exported to excel without omitting 0's.

Below code only exports 11 to the excel cell without the 0's.
xlsWorkSheet.cells().item(row,1).value(xxxx.abc);

Any help is much appreciated.

Thank you

  • WillWU Profile Picture
    WillWU 22,350 on at
    RE: Text Formatting of Excel cells to export data through code

    Hi Raghavendra,

    Actually when you export data to excel, text will remain text, numbers will remain numbers, you could open the excel with text to check.

    Did you try to change the format of the cells to text?

  • RE: Text Formatting of Excel cells to export data through code

    Thanks Will. But any idea how we could go about this when the field length is not fixed?

  • Verified answer
    WillWU Profile Picture
    WillWU 22,350 on at
    RE: Text Formatting of Excel cells to export data through code

    Hi Raghavendra,

    Is the length of your field fixed?

    You could try to set the format like this in Excel.

    pastedimage1594088962264v1.png

  • RE: Text Formatting of Excel cells to export data through code

    Hi Will,

    Thanks for your response.

    Yes the field mentioned is a String field in AX. Let's say the field has data like 000123, 000011, XYZ etc.

    I have below concerns:

    1. Formatting the cell to TEXT in excel after data is exported won't bring back the 0's.

    2. I have referred to the link you shared above. It mainly focuses on number formatting and other type conversions used to extract the data needed to file.

    In my case, I don't need any type conversion as the field is STRING.

    Please help me if you know how to format the column to TEXT by x++ code before extracting data?

    xlsWorkSheet.cells().range(strFmt("H%1:H%1",row)).numberFormat("00000000000000");

    Can the above be somehow applied to TEXT formatting? I tried but to no avail.

    Thanks

  • WillWU Profile Picture
    WillWU 22,350 on at
    RE: Text Formatting of Excel cells to export data through code

    Hi Raghavendra,

    What's type of the field "xxxx.abc"?

    If it is a string field, you just need to set the format of the column in Excel.

    https://community.dynamics.com/ax/b/hellodax/posts/formating-cells-when-export-to-excel-example-code

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans