-1

I am updating some records in my Batch Apex. I want to add those all records in some excel file and mail it. I am able to do this if i add all those details in a csv file instead of xml file by using below code

String csvString = 'Contact Id, Name, Account Name, Account.Contact Owner, Previous Contact Owner, New Contact Owner\n';
        
for (Contact cont: modifiedCont)        
{
    String contDetail = cont.Id + ', ' + cont.Name + ', ' + cont.Account.Name + ', ' + cont.Account.Contact_owner__c + ', ' + priorCont.get(cont.OwnerId).OwnerId + ', ' + cont.OwnerId + '\n';
    csvString = csvString + contDetail;
}

// Create CSV File as Blob
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
Blob csvBlob = Blob.valueOf(csvString);

csvAttc.setFileName('report.csv');
csvAttc.setBody(csvBlob);

// Create Email Object
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] sendTo = new List<String> {'[email protected]'};
email.setSubject('Contact Owner Update Report');
email.setPlainTextBody('This is the report of the contacts which got updated in batch run');email.setToAddresses(sendTo);email.setFileAttachments(new Messaging.EmailFileAttachment[] {csvAttc});

Messaging.SendEmailResult[] res = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

Because of some requirements i need these records in an excel file instead of csv file. Is there a way to achieve this without using some third party library.

1 Answer 1

2

Technically yes, but practically no.

Excel files are relatively complex beasts following the Office Open XML SpreadsheetML format that are put into a Zip archive.

Salesforce doesn't provide any class/code to zip/unzip data, and while there are ports of other Zip libraries in Apex the governor limits (specifically the CPU and Heap limits) make it impractical to generate or work with data of any appreciable size.

Force.com isn't a platform for general computation. If you really need to do this, the usual recommendation is to use client-side JS or another platform like Heroku (or really anything that you can make a callout to or use a Connected App to get API access to Salesforce for).

You must log in to answer this question.

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