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 :
Microsoft Dynamics CRM (Archived)

Create Excel Sheet from DataTable in Custom Workflow

(0) ShareShare
ReportReport
Posted on by

Hello,

I'm trying to fetch some data from the CRM, insert it into an excel sheet and attach it to an email. 

I was able to retrieve the fetchxml data and convert it to DataTable. But I couldn't find a permitted way to convert the DataTable to Excel Sheet. 

Here's my code so far: 

 Entity EmailAttachment = new Entity();
            EmailAttachment.LogicalName = "activitymimeattachment";
            EmailAttachment["objectid"] = new EntityReference("email", EmailId);
            EmailAttachment["objecttypecode"] = "email";
            EmailAttachment["subject"] = "Test Attachment";

            string fetchxml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                  <entity name='contact'>
                    <attribute name='fullname' />
                    <attribute name='telephone1' />
                    <attribute name='contactid' />
                    <order attribute='fullname' descending='false' />
                    <filter type='and'>
                      <condition attribute='fullname' operator='like' value='%Test%' />
                    </filter>
                  </entity>
                </fetch>";

            
            System.Data.DataTable resultTable = ExecuteFetchXml(fetchxml, service);

            // how to convert resultTable to Excel file

            EmailAttachment["body"] = // The Attachment should go here
            EmailAttachment["filename"] = "TestAttachment.xlsx";

            Guid attachmentId = service.Create(EmailAttachment);


*This post is locked for comments

I have the same question (0)
  • Verified answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi SB,

    The easy way it to create a CSV since Excel opens it. If you NEED it to be an Excel file you could use something like this: msdn.microsoft.com/.../x80526fk.aspx

    Hope this helps! I'd appreciate if you'd mark this as Answering your question.

    Thanks,

     Aiden

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    use openXML to create the excel file and then convert that excel to base64 and pass it to body, refer the below link for how to use openXML :

    www.c-sharpcorner.com/.../how-to-create-excel-file-using-C-Sharp

  • Community Member Profile Picture
    on at

    Thanks very much Aiden and Mohit for your replies.

    I went through the creation of Excel File steps as mentioned, but i'm stuck in converting the excel to base64, can you provide a sample code on how to do this ?

    I appreciate your help

  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    What's the format of the Excel object you have? If it's a byte[], you can set that using:

    string documentbody = System.Convert.ToBase64String(fileData);

    then set that on your note.

  • Community Member Profile Picture
    on at

    Thank you Aiden. It's in this format:

    Microsoft.Office.Interop.Excel.Workbook workbook;

    Can this be converted to Byte[] ?

  • Suggested answer
    Community Member Profile Picture
    on at

    but why you want to convert in byte ? convert it to string as you need to pass in the

    EmailAttachment["body"]

  • Suggested answer
    Community Member Profile Picture
    on at

    refer the below link  :

    lakshmanindian.wordpress.com/.../attachments-in-microsoft-dynamics-crm-2011

  • Community Member Profile Picture
    on at

    Oh, I wasn't sure it was possible. Can you possibly provide sample code on how to convert a workbook to string?

    I appreciate your help !

  • Community Member Profile Picture
    on at

    Thank you for the link, but I believe FileStream that is used in the Excel example in that link wouldn't work within CRM Workflow.

  • Aiden Kaskela Profile Picture
    19,696 on at

    SB (and others),

    You'll want to convert the object to a byte[] (which is usually pretty easy), then convert the byte[] to a base 64 string. I'm looking at how to do the first part now.

    Thanks,

     Aiden

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans