^ Click Here

Saturday, January 14, 2012

To get a list in different format as HTML / excel / pdf / word in Salesforce

More often than not we need to have an option where we can get a list in an excel or HTML or pdf format....specially for sending it in e-mails.

These formats are standards and presentable hence much desired most often than not.

I did an example where I can show a list of Contacts, Owner name and corresponding Accounts Name with the option to retrieve/download it in excel/HTML/pdf/word format (I tried for picture format also but it didn't work)

Things to notice are

- The file is downloaded with the current date in the name for the ease of distinguishing.

- In the controller the transient variable is being used so that it can show maximum number of records without the view-state error.

- The table is given different background colors for differentiation between rows but each format reads it differently so there is a variation.

Also this is just a raw presentation and a starter.....much can be improved upon this and different functionality or options can be added/modified into it.....

Click here for a demo of this.

The page (visualforce) code -

<apex:page controller="ContactListController"
    contentType="{!CASE($CurrentPage.parameters.format,'excel','application/vnd.ms-excel#Contact_List_'+TEXT(NOW())+'.ms-excel','powerpoint','application/vnd.ms-word#Contact_List_'+TEXT(NOW())+'.ms-word','html','text/html#Contact_List_'+TEXT(NOW())+'.html','image','image/jpeg','')}"

    renderAs="{!IF($CurrentPage.parameters.format == 'pdf','pdf','')}"
    showHeader="false" sidebar="false">
    <style>
.oddclass {
    background: lightskyblue;
}

.evenclass {
    background: white;
}
</style>
    <apex:pageBlock title="Contact Table" tabStyle="Contact">
        <apex:pageBlockButtons dir="RTL"
            rendered="{!$CurrentPage.parameters.format == null}">

            <apex:form>
                <apex:commandLink action="{!pageOut}" value="Excel Output"
                    target="_blank" style="margin:5px;">
                    <apex:param name="format" value="excel" />
                </apex:commandLink>
                <apex:commandLink action="{!pageOut}" value="HTML Output"
                    target="_blank" style="margin:5px;">
                    <apex:param name="format" value="html" />
                </apex:commandLink>
                <apex:commandLink action="{!pageOut}" value="PDF Output"
                    target="_blank" style="margin:5px;">
                    <apex:param name="format" value="pdf" />

                </apex:commandLink>
                <apex:commandLink action="{!pageOut}" value="Image Output"
                    target="_blank" style="margin:5px;">
                    <apex:param name="format" value="image" />
                </apex:commandLink>
                <apex:commandLink action="{!pageOut}" value="Word Output"
                    target="_blank" style="margin:5px;">
                    <apex:param name="format" value="powerpoint" />
                </apex:commandLink>

            </apex:form>
        </apex:pageBlockButtons>
        <apex:pageblockTable value="{!ContactList}" var="cont"
            rowClasses="oddclass,evenclass" border="1" cellpadding="2"
            cellspacing="1" style="background:mediumspringgreen;">
            <apex:column value="{!cont.Name}" />
            <apex:column value="{!cont.Owner.Name}" headerValue="Owner Name" />
            <apex:column value="{!cont.Account.Name}" />
        </apex:pageblockTable>
    </apex:pageBlock>
</apex:page>


The Controller (Apex) Code -

Public Class ContactListController{
    transient Public List ContactList;
    public pagereference pageOut(){
       String format = ApexPages.currentPage().getParameters().get('format');
       pagereference pageRef = ApexPages.currentPage();
       pageRef.getParameters().put('format',format);
       pageRef.setRedirect( false );
       return pageRef;
   }
   public List getContactList(){
       ContactList = [select id, name, owner.name, Account.Name from Contact];
       if(ContactList.size() != 0)
        return ContactList;
       else
        return null;
   }
}




No comments:

Post a Comment