-2

could you please help me how to call report Xlsx file format, i am tried to below code, but i am not get the result:

public class BatchUtilClass {
    @future(callout=true)
    public static void futureMethod() {
        list <Report> reportList = [SELECT Id,Name FROM Report WHERE Name = 'users logins' limit 1];
        system.debug('list'+reportList);
        String reportId = (String)reportList.get(0).get('Id');
        system.debug('reportids'+reportId);
        ApexPages.PageReference report = new ApexPages.PageReference('/00O3N000000097hg9v?csv=1&isdtp=p1');
        // ApexPages.PageReference report = new ApexPages.PageReference('/00O3N000000097hg9v?export=1&enc=UTF-8&xf=xls');
        system.debug('pagereport'+report);
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        system.debug('atachment'+attachment);
        attachment.setFileName('Report.csv');
        // attachment.setFileName('Report.xlsx');
        attachment.setBody(report.getContent());
        attachment.setContentType('text/csv');
        // attachment.setContentType('text/xlsx');
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
        message.setSubject('users logins Report');
        message.setPlainTextBody('The testing report is attached.');
        message.setToAddresses( new String[] { 'hkvts2@gmail.com' } );
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );

    }
}
  • (a) No need to beg in comments--we're here to help you, and (b) "not get the result" doesn't tell us how to help you. What problems are you having? – sfdcfox Nov 12 at 3:57
  • when i am trying to scheduling this above class ,i am not get the report file in xlsx .xlsx format code is comments in code – HKV Nov 12 at 4:01
  • @KushalChauhan Can you clarify how that applies? That question is CSV format. – David Reed Nov 12 at 4:40
  • I am using xlsx format:: ApexPages.PageReference report = new ApexPages.PageReference('/00O3N000000JYObUAO?export=1&enc=UTF-8&xf=xlsx'); – HKV Nov 12 at 4:47
0

Your code is not asking for XLSX format. You're asking for either CSV or XLS format, depending on which lines are commented out. Changing the filename or MIME type on the attachment object has no effect on what file format is produced.

Inspecting the URL generated when exporting an XLSX-format report in Lightning yields the URL format

https://<mydomain>.my.salesforce.com/servlet/PrintableViewDownloadServlet?isdtp=p1&reportId=<report id>

Presumably, you'd need to ask for that URL to obtain the desired format.

0

Based on this answer, the correct way to set the Content Type for an xlsx file is to use application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, not text/xlsx.

  • I am tryied this one but once download file, file format is not matched. – HKV Nov 12 at 4:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.