I have created an excel from a visual force page by using .....

it gets downloaded as .xls file.

now I want to attach this file to an email template. how can I do so?

2 Answers 2


You can put your visualforce page inside a visualforce email template like: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_attachments.htm

You can change the render type for the attachments just like in your visualforce page like this example for PDF:

<messaging:attachment renderAs="PDF" filename="Offerte {!relatedTo.Account.Name}.pdf">
  • I don't want pdf I want excel.
    – ajay rawat
    Commented Sep 23, 2016 at 2:20
  • 2
    I understand. than it is: <messaging:attachment contentType="application/vnd.ms-excel#Excel.xls" cache="true">
    – Henk3000
    Commented Sep 26, 2016 at 11:52

Here is a example from Salesforce where a page can be send as pdf in email attachment. Similarly you can do for Excel. Hope this helps.

// Reference the attachment page, pass in the account ID
PageReference pdf = Page.attachmentPDF;

// Take the PDF content
Blob b = pdf.getContent();

// Create the email attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
  • it don't work for excel as contenttype="application/x-excel#FILENAME.xls". this it download the excel.
    – ajay rawat
    Commented Sep 22, 2016 at 14:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .