How to download Rich Text Area image from Salesforce to Excel File

In this post, we will talk about how to download rich text images into excel files. Sometimes our clients want to download Contacts or Accounts details into excel files but If records contain any rich text area field which holds images then it might create problems. If you follow the normal procedure then it will not display the image in the excel file. I will share with you how you can achieve this task.

Requirements: Download Rich Text Area image

  • Basic Knowledge of Salesforce public site creation.
  • Knowledge of VisualForce pages and Apex.

Salesforce stores richtext fields images in ContentReference object, but we can not query or see this object from anywhere. If you ever try to print rich text area image field value you will see output like this 

download Rich Text Area image from Salesforce to Excel File

Here eid is a custom object, the “entity ID”. Presumably, you could use this to view an instance of your record and feoid is the “field entity ID”. This describes the field that the content is stored in. You can see this value when you’re looking at the field in the Setup screen and refid is an ID of the type ContentReference

Steps

  1. Create a site in salesforce. As we know that our Rich text area data is saved on the RTA server so we can not directly access it or share it publicly so, create a site in salesforce. You can see the sites option in Setup of salesforce.
  2. Allow read access of your Object and Rich Text Field in the Public Access Setting of your site. For example Account object have a rich text area field with name Excel Image, so after allowing it in public access setting it looks like this



    And Field

  3. Create a sharing rule related to your Object and select Shared With Default Help Center Site Guest User
  4. Replace your RTA Image machine URL with your site URL. You must only replace the front portion (till .com) of RTA image with your site link. for ex
    suppose ABC.RichTextImage__c field value is https://cunning-impala-iuaae5-dev-ed–c.documentforce.com/servlet/rtaImage?eid=0015j000003ko&feoid=00N500000JEP1Q

    And Your Site url is https://cunning-impala-iuaae5-dev-ed-180f030a0a8.ap27.force.com
    so final url must be looks like this https://cunning-impala-iuaae5-dev-ed-180f030a0a8.ap27.force.com/servlet/rtaImage?eid=0015j000003ko&feoid=00N500000JEP1Q
  5. Now you can use this updated URL in apex image tag to render your image in table column for Ex:

VF Page

<apex:page standardController="Account" contentType="application/vnd.ms-excel#TESheet.xls"  extensions="ExcelController"  >
    <!--contentType="capplication/vnd.ms-excel#TESheet.xlsx"
  contentType="application/vnd.ms-excel#TESheet.xls"
contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet#MyFile.xls"
contentType="application/vnd.ms-excel; charset=Windows-1151#TESheetDownload.xls"
-->
    <style>
        img{
        height:100px;
        width: 100px;
        }
        
    </style>
    <apex:outputPanel >
        <table id="aTable" cellpadding="10px" style="width: 100%; border-collapse: collapse;text-align: center; " border="1">
            <thead>
                <tr>
                    <th style="text-align:center;background-color:#e3e5e9" >User Name</th>
                    <th style="text-align:center;background-color:#e3e5e9" >User Logo</th>
                </tr>
            </thead>
            <tbody>
                <apex:repeat value="{!lsOfAcc}" var="a"  >
                    <tr >
                        <td > {!a.name}</td>
                         <td  >
                             <apex:image id="theImage" width="65" height="35" url="{!a.logo__c}" />
                        </td>
                    </tr>
                </apex:repeat>   
            </tbody>
        </table>
    </apex:outputPanel>
    
</apex:page>

ExcelSheetController.apxc

public class ExcelController {
    public List<Account> lsOfAcc{get;set;}
    
    
    public ExcelController(ApexPages.StandardController controller) {
        
        String name = 'Test%';
        String siteLink ='https://curious-bear-9xporm-dev-ed.trailblaze.my.salesforce-sites.com';        
          lsOfAcc = [Select Id, Name,logo__c from Account Where Name Like :name];
		  for(Account asp : lsOfAcc){
            System.debug('asp'+asp);
            if(String.isNotBlank(asp.logo__c)){
                String temp = (''+asp.logo__c);
                temp = temp.subString(temp.indexOf('"'),temp.indexOf('"', temp.indexOf('"')+1)).replace('amp;','').replace('"','');
                asp.logo__c = siteLink + temp.subString(temp.indexOf('.com')+4,temp.length());	
                System.debug('Temp is '+asp.logo__c);
            }         //
          }
      }
    
    
    
}

You can see a demo code here 

nischal vasisth

nischal vasisth

Nishchal Vashisht, 2x certified Developer at cloud certitude

Share this article

Leave a reply

Subscribe for Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,541 other subscribers

Our Supporter

RECENT POSTS

Apex Hours

Apex Hours is one stop platform to learn Salesforce skills and technology

Join our Newsletter and get tips and tricks how to explore the salesforce for free!