I had used in the controller:


but when i clicked a button to send the email with an attachment, i am getting all fields in one row.

How can i solve this issue?

2 Answers 2


Setting the ContentType doesn't perform any format conversion. A non xls attachment sent in this way will just have all the HTML dumped into the body of the spreadsheet.

I think the only way to get xls files from Salesforce is either:

  1. from a Custom Report, where you know the Id and can form a download URL like /00OG0000005YaRh?export&xf=xls

  2. or by hand-generating output tags compatible with the the office XML format from a Visualforce Page.

  • 2
    #1 is not really a true Excel format, the program still complains that it was given a HTML file when you open it (but it does come pretty close and bit better than CSV). Force open it in text editor to inspect the source.
    – eyescream
    Commented Dec 23, 2013 at 11:52
  • I just died inside :( thanks @eyescream, good spot Commented Dec 23, 2013 at 11:54

I've been asked to try to generate "true" XLSX format as 2nd pass to make Scheduled reports as attachment even friendlier. For sure it's not worth trying to emulate the old xls (binary) format...

I've spent some time searching and so far this thing looks most promising: http://neerajgupta999.blogspot.in/2012/06/multisheet-excel-from-apex.html.

If I'll have "version 2" of either the report export hack or something workable I promise to get back here and share ;)

You must log in to answer this question.

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