-3

I'm sending an email via scheduler class with an excel file as an attachment, but the data in the file does not appear to be in proper format. Please check the following image: enter image description here

The code I'm using to generate the file:

enter code here 
private static void sendMailWithAttachment() {

    String subject = 'excel file subject';

    String body = 'excel file body';

    Messaging.SingleEmailMessage emailMessage = new Messaging.SingleEmailMessage();

    emailMessage.subject = subject;

    emailMessage.plaintextbody = body;

    Messaging.EmailFileAttachment excelAttachment = new Messaging.EmailFileAttachment();

    excelAttachment.setFileName('excel_report' + '.xls');

    excelAttachment.setBody(reportPage.getContent());

    excelAttachment.setContentType('application/vnd.ms-excel');

    List<Messaging.EmailFileAttachment> attachments = new List<Messaging.EmailFileAttachment>();

    attachments.add(excelAttachment);

    emailMessage.setFileAttachments(attachments);

    Messaging.sendEmail(emailMessage);

}

1.23457E+14 is not a proper format.

How to fix the format of the number through apex?

1
  • 2
    That's Excel formatting. It doesn't seem to have anything to do with Apex.
    – Adrian Larson
    Sep 14 '20 at 18:07
2

The general answer is "you can't". Excel (and other document formats) are complex, and there aren't any (or are only very few, and quite limited) tools on-platform that can manipulate such documents. We only have a basic ability to create such documents through setting the content-type (as you are doing).

The formatting you're seeing is auto-applied by Excel based on its detection of the type of data.

1
  • Side note: Excel doesn't handle numbers larger than 15 digits. I've run up against this on several occasions due to design choices made by another system. The workaround is to go to Data -> Get Data in excel, load the csv (or what have you), and then click the "transform" button to open up the power query editor so you can force excel to not treat it like a number.
    – Derek F
    Sep 14 '20 at 19:08

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