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 :
Customer experience | Sales, Customer Insights,...
Answered

Export data in excel sheet on button click

(0) ShareShare
ReportReport
Posted on by 105

Hello,

I have created a button(Approve) on account entity. If I click on this button it should export data in excel format with field name as column heading. I have written JS code to achieve this requirement.

var accName=Xrm.Page.getAttribute("name").getValue();

var data = [[accName]];
var csvContent = '';
data.forEach(function (infoArray, index) {
dataString = infoArray.join(';');
csvContent += index < data.length ? dataString + '\n' : dataString;
});

var download = function(content, fileName, mimeType) {
var a = document.createElement('a');
mimeType = mimeType || 'application/octet-stream';

if (navigator.msSaveBlob) { // IE10
return navigator.msSaveBlob(new Blob([content], { type: mimeType }), fileName);
} else if ('download' in a) { //html5 A[download]
a.href = 'data:' + mimeType + ',' + encodeURIComponent(content);
a.setAttribute('download', fileName);
document.body.appendChild(a);
setTimeout(function() {
a.click();
document.body.removeChild(a);
}, 66);
return true;
} else { //do iframe dataURL download (old ch+FF):
var f = document.createElement('iframe');
document.body.appendChild(f);
f.src = 'data:' + mimeType + ',' + encodeURIComponent(content);

setTimeout(function() {
document.body.removeChild(f);
}, 333);
return true;
}
}

download(csvContent, 'csv file.csv', 'text/csv');

And the result is:

Screenshot-_2800_3_2900_.png

It is working fine but how to display fields name too..? or Is there any other way to achieve this requirement? like flow or any other?

Thanks in Advance.

I have the same question (0)
  • cloflyMao Profile Picture
    25,210 on at
    RE: Export data in excel sheet on button click

    Hi nanu2008,

    1. You can extract account name from "When a record is updated".

    In my demo, I extract the value from "When a record is selected":

    6014.JPG

    2. We should use following filter to find label of the option, ${optionsetvalue} is from the beginning action.(When xx is selected or updated.)

    Set the limit count to 1.

    objecttypecode eq 'account' and attributename eq 'industrycode' and attributevalue eq ${optionsetvalue}
    
    

    1452.JPG

    Due to we use "List records" action to find label of option, the result will be a collection,

    so we need to use an expression to only extract the first item and create a variable to save the label value,

    body('Retrieve_label_of_option')?['value'][0]?.value

    finally apply the variable to "Create CSV" action:

    pastedimage1607335393295v2.png

    Result

    pastedimage1607335426414v3.png

    You could refer to following tutorial to get the first record from the list:

    https://crmtipoftheday.com/1256/get-attribute-of-the-first-record-from-the-list/

    Regards,

    Clofly

  • _nanu2208 Profile Picture
    105 on at
    RE: Export data in excel sheet on button click

    Hi Clofly,

    1.How can we set filename as a account name. For ex. accountname.csv. I'm using SharePoint path in flow. 2. I'm getting value of Option set(Industry and relationship Type) instead of label. I have tried this solution( https://community.dynamics.com/365/f/dynamics-365-general-forum/392431/setting-text-for-option-set-fields-in-flow-email-step ) but didn't get result. How can I achieve this?

    Screenshot-_2800_58_2900_.png

    My Flow:

    1. Screenshot-_2800_64_2900_.png

    2.Screenshot-_2800_65_2900_.png

    3.Screenshot-_2800_66_2900_.png

    4.Screenshot-_2800_67_2900_.png

    5.Screenshot-_2800_68_2900_.png

  • _nanu2208 Profile Picture
    105 on at
    RE: Export data in excel sheet on button click

    Hi,

    Okay. Thanks for the quick reply.

  • cloflyMao Profile Picture
    25,210 on at
    RE: Export data in excel sheet on button click

    Hi nanu2208,

    Currently flow only supports to save file to OneDrive or SharePoint(remote server), if it is required to download the file to local machine, I suggest you keep working with JavaScript approach.

    Regards,

    Clofly

  • _nanu2208 Profile Picture
    105 on at
    RE: Export data in excel sheet on button click

    Hi,

    Thanks for the reply. I have created flow like above. Can we download this file in our machine using flow?

  • Verified answer
    cloflyMao Profile Picture
    25,210 on at
    RE: Export data in excel sheet on button click

    Hi nanu2208,

    As per my understanding, you may want to display field name as row header.

    If so, you just need to insert field as first array item to the data variable.

    var data = [
        ["Account Name"],
        [accName]
    ];

    pastedimage1606896428654v1.png

    Obviously, it will be easier if using flow.

    1. Select "Create a flow" of Flow button in ribbon, by doing that we will get the trigger "When a record is selected".

    2. Get fields value of the selected account in array format.

    3. Select "Create CSV table" action to create a csv table by using array format result of action 2.

    4. Create a CSV file in OneDrive from the output of action 3.

     pastedimage1606897307033v2.png

    In Account form, perform the flow.

    pastedimage1606897656268v3.png

    The result is same to the JavaScript approach.

    Regards,

    Clofly

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daniyal Khaleel Profile Picture

Daniyal Khaleel 141

#2
DAnny3211 Profile Picture

DAnny3211 134

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 70 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans