+ Start a Discussion
Rajesh SFDCRajesh SFDC 

how to get visualfoce page as excel format to mail attachment in apex

here my code given below:
public Class GenerateCSV 
{
   public PageReference SendEmail() 
      {
            Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
            attachment.setFileName('Report.pdf');
             PageReference csvPage = Page.GenerateCSV;  
            Blob csvBlob = csvPage.getContent();
            attachment.setBody(csvBlob);
            //attachment.setContentType('pdf');
            attachment.setContentType('application/vnd.ms-excel');
            Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
            message.setSubject('Report');
            message.setPlainTextBody('The report is attached.');
            message.setToAddresses( new String[] { 'rajesh87sfdc@gmail.com' } );
            Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
            Return null;
            
         }
}

here i am getting only blank excel in mails