function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jagadeesh AngadiJagadeesh Angadi 

How to export non-printable report into .XLSX format in APEX?

I am trying to export non-printable report into .XLSX format and send it to external users via email. Exporting to CSV works fine, but when I try to export the same with .XSLX format, I am getting the attachment either in corrupt state or an unformatted state.

Is it possible to export a report in .xslx format?

Below is my code snippet, I am trying out with different combinations of report URL and content type, but none of them worked. Please help if there is any way forword for this?
String reportId = '00O1700000*****';
String instanceName = System.URL.getSalesforceBaseUrl().toExternalForm();
string url=instanceName+'/'+reportId+'?excel=1'; // Not working
//string url=instanceName+'/'+reportId+'?export&xf=xls'; // Not working
//string url=instanceName+'/'+reportId+'?csv=1&isdtp=p1'; //Not working
ApexPages.PageReference report = new ApexPages.PageReference(url);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
List<Messaging.EmailFileAttachment> csvAttcList = new List<Messaging.EmailFileAttachment>();
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
String mailBody = 'Please find the attachment for daily case report.';
csvAttc.setFileName('Daily Case Report.xlsx');
csvAttc.setBody(report.getContent());
csvAttc.setContentType('text/vnd.openxmlformats'); //Not working
//csvAttc.setContentType('text/csvs'); //Not working
//csvAttc.setContentType('application/vnd.ms-excel'); //Not working
csvAttcList.add(csvAttc);
email.setSubject('Daily Case Report on '+System.now());
List<String> toAddress = new List<String>();
toAddress.add('email@gmail.com');
email.setToAddresses( toAddress );
email.setPlainTextBody(mailBody);
email.setFileAttachments(csvAttcList);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

 
Shri RajShri Raj

Instead of using the report URL and trying to set the content type to different values, you can use the Salesforce API to directly export the report in .xlsx format. One way to do this is to use the Export method of the AnalyticsAPI class, which allows you to export a report in various formats, including .xlsx.

// Define the report ID and export format
String reportId = '00O1700000*****';
String exportFormat = 'xlsx';

// Create an instance of the AnalyticsAPI class
AnalyticsAPI analyticsAPI = new AnalyticsAPI();

// Use the Export method to export the report
Blob reportBlob = analyticsAPI.Export(reportId, exportFormat);

// Create the email attachment
Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
attachment.setFileName('Daily Case Report.xlsx');
attachment.setBody(reportBlob);
attachment.setContentType('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

// Create the email and attach the report
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setSubject('Daily Case Report on ' + System.now());
email.setToAddresses(new List<String> { 'email@gmail.com' });
email.setPlainTextBody('Please find the attachment for daily case report.');
email.setFileAttachments(new List<Messaging.EmailFileAttachment> { attachment });

// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
``