0

I'm sending an email through the batch class with CSV file as an attachment, but the data alignment is wrong after generating CSV, I want the same alignment for all the columns. Please check the following image: enter image description here

my code is as follows to generate CSV:

    for(Account objAccount : accList){

      if(purchaseMap.containsKey(objAccount.Region_Mincron__c) ){
         String records = objAccount.Beacon_Acc_Number__c+',';
         if(objAccount.Name.contains(',')){
            String temp = objAccount.Name.remove(',');
            records+=temp.trim()+',';
         }else{
            records+=(objAccount.Name).trim()+',';
         }
         records+=(objAccount.Days_Since_Last_Purchase__c).trim()+',';
         Date d = objAccount.Date_Last_Purchase__c;
         //records+= objAccount.Date_Last_Purchase__c;
         records+=d.day()+'/'+d.month()+'/'+d.year()+',';
         records+=objAccount.Owner.Name+',';
         records +='\n';
         String tempStr = '';
         if(emailFileMap.containsKey(objAccount.Region_Mincron__c) && emailFileMap.get(objAccount.Region_Mincron__c) != null){
            tempStr = emailFileMap.get(objAccount.Region_Mincron__c);
            emailFileMap.put(objAccount.Region_Mincron__c,tempStr+records);
         }else{
            emailFileMap.put(objAccount.Region_Mincron__c,finalstr+records);
         }
      }
   }
  }    

  global void finish(Database.BatchableContext bc){      
   List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for(String regionStr : purchaseMap.keySet()){
      Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
      blob excel;
       if(emailFileMap.containsKey(regionStr) && emailFileMap.get(regionStr) != null){
         excel = Blob.valueOf(emailFileMap.get(regionStr));
       }
       System.debug('regionStr--->'+regionStr);
       //blob excel = blob.valueOf(finalstr);
       attach.setBody(excel);
       attach.setFileName(regionStr+' file.csv');
       String subject = regionStr;
       //String body = 'Users who are not logged in since last 30 days';
       String body = 'Please find attached file for purchase details';
       String[] addressFromLabel;
       if(purchaseMap.get(regionStr) != null)
        addressFromLabel = purchaseMap.get(regionStr);

       Messaging.singleEmailMessage Emailwithattch = new Messaging.singleEmailMessage();

       Emailwithattch.setSubject(subject);

       Emailwithattch.setToaddresses(addressFromLabel);

       Emailwithattch.setHtmlBody(body);     
       if(attach.getBody() != null){
         Emailwithattch.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
         emails.add(Emailwithattch);
       }



    }
      Messaging.SendEmailResult [] r =

      Messaging.sendEmail(emails);
  }

Can we fix alignment through apex?

1 Answer 1

3

This is an locale + Excel issue. You cannot apply formatting in a CSV file as such.

You're outputting dates in DD/MM/YY format. Excel is reading them in MM/DD/YY. If you notice which dates are and are not aligning "correctly", you'll see that the ones left-aligned (i.e., not treated as a valid date in Excel) have a DD value that is greater than 12, and is not therefore a valid date in the US locale.

You'll need to either change your date format, or configure Excel to interpret dates in DD/MM/YY.

You must log in to answer this question.

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