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.