1

Is there any apex code that a generates a csv file of data where a particular data is being highlighted or the background color of the cell where the data is present could be changed by the apex code which generates that .csv file?

my code which generates the csv file

List<VanaHCM__Absence_Request__c> ARlist = [Select name, VanaHCM__Absence_Type_Request__r.name, VanaHCM__Dump_Start_Date__c, VanaHCM__Dump_End_Date__c, VanaHCM__Actual_Absence__c, VanaHCM__Approved__c
                                            From VanaHCM__Absence_Request__c 
                                            WHERE ((VanaHCM__Dump_Start_Date__c >= 2015-01-18 AND VanaHCM__Dump_Start_Date__c <= 2015-01-23) OR (VanaHCM__Dump_End_Date__c >= 2015-05-18 AND VanaHCM__Dump_End_Date__c <= 2015-05-23))
                                            AND (VanaHCM__Requested_By__r.Vanahcm__User__r.userrole.name LIKE '%ford%' or VanaHCM__Requested_By__r.Vanahcm__User__r.userrole.name LIKE '%quality%' or VanaHCM__Requested_By__r.Vanahcm__User__r.userrole.name LIKE 'LF%')
                                            order by VanaHCM__Dump_Start_Date__c Limit 100];
string header = 'Absence Request Id, Absence Type, Start Date, End Date, Total Absence Requested, Approved \n';
string finalstr = header ;
for (VanaHCM__Absence_Request__c AR :ARlist)
{
       string recordString = AR.Name+','+AR.VanaHCM__Absence_Type_Request__r.name+','+AR.VanaHCM__Dump_Start_Date__c+','+AR.VanaHCM__Dump_End_Date__c+','+AR.VanaHCM__Actual_Absence__c+','+AR.VanaHCM__Approved__c +'\n';
       finalstr = finalstr +recordString;
}
Messaging.EmailFileAttachment csvAttc = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(finalstr);
string csvname= 'Absence_Request.csv';
csvAttc.setFileName(csvname);
csvAttc.setBody(csvBlob);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] toAddresses = new list<string> {'bhxxxxx@xxxxx.com'};
String subject ='Absence_Request CSV';
email.setSubject(subject);
email.setToAddresses( toAddresses );
email.setPlainTextBody('Absence_Request CSV ');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAttc});
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
  • 1
    Hi, welcome to SFSE. if you can post what you have tried and where you got stuck, we can help.. its very hard for us to give answers if the question is too broad & open ended without any attempts or code references. – Vamsi Krishna Gosu Jul 24 '15 at 5:25
  • Hi vamsi - this is my code which generates a csv file when i execute it and in this i need a picee of code which will help me highlight the words "LWP" in red color and "NO" in orange color – Nemesis88 Jul 24 '15 at 6:07
1

CSV(Comma Seperated Values) is a file format which stores values with a seperation of comma to distinguish between first value and second value and each row is distinguished by carriage return.

CSV files are interpreted differently by different programs. For example, Excel will show the CSV file as Row Column pattern. But a text editor will display the CSV file just with commas without Row Column pattern.

So you can't apply any formatting to the CSV file by any means.

Hope it helps.

| improve this answer | |
  • Thanks for the help vigneshwaran, but i assume that when i add this piece of code to my original one, the csv file generated gets changed totally to a smaller font size. – Nemesis88 Jul 24 '15 at 6:16
  • else{recordString +=',<span style="color:#THE_COLOR#;font-weight:bold;">'+'LWP'+'</span>';} else{recordString +=',<span style="color:#THE_COLOR#;font-weight:bold;">'+'LWP'+'</span>';} – Nemesis88 Jul 24 '15 at 6:17
  • Have you tried opening the CSV in notepad and notice the size change? – Vigneshwaran G Jul 24 '15 at 6:38
2

csv file doesn't support any formating like cell background color etc. In excel when you do formatting on CSV file and save, all the formatting is lost.

| improve this answer | |

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.