Generate Excel sheet with a dynamic name in visualforce

In visualforce we can get the output in excel form by specifying the content type in the page component. By specifying the content type as 'application/vnd.ms-excel#FILENAME.xls' the viusalforce will always be rendered as excel sheet.

As an example below visualforce page will give an output of excel sheet with the name of the sheet being 'myexcelreport'

Visualforce Page
 


Apex Class

In this case the name of the sheet will remain static and every time you extract the sheet it will be named as 'myexcelreport'. At times we may need the excel sheet name that changes every time with certain criteria's for example date. That is we may need the name of the excel sheet be dynamic as per our requirements. We can achieve this by simply concatenating the formula in contenttype attribute with curly bracket and ! as done in below example-

Apex Class
 
In this case the output excell sheet will have name depending on the date, ex- 'Report- 08_12_2014'

Dynamically add or remove rows in a visualforce table

Visualforce can be leveraged to implement user interfaces as per our requirements, many times we need solutions that are dynamic enough that would cater to our future needs. Here is a visualforce application that allows dynamic addition or removal of table rows. Example uses wrapper class in building the logic. Cross symbol and addition symbol are used to add or remove rows.



Visualforce Page

<apex:page StandardController="Account" extensions="MultiAdd" id="thePage">

<apex:form >

<apex:pageblock id="pb" >

    <apex:pageBlockButtons location="bottom">        

        <apex:commandbutton value="Create Contacts" action="{!Save}"/>

    </apex:pageBlockButtons>            

        <apex:pageblock id="pb1">                                    

                <apex:pageblocktable value="{!lstInner}" var="e1" id="therepeat">

                   <apex:column >

                    <apex:facet name="header">Account</apex:facet>

                     <apex:inputfield value="{!e1.con.accountid}"/>

                   </apex:column>

                   <apex:column >

                   <apex:facet name="header">Name</apex:facet>

                     <apex:inputtext value="{!e1.con.name}"/>

                   </apex:column>

                   <apex:column >

                   <apex:facet name="header">Email</apex:facet>

                     <apex:inputfield value="{!e1.con.email}"/>

                   </apex:column>

                   <apex:column >

                   <apex:facet name="header">Phone</apex:facet>

                     <apex:inputfield value="{!e1.con.Phone}"/>

                   </apex:column>

                   <apex:column >

                   <apex:facet name="header">Del</apex:facet>

                      <apex:commandButton value="X" action="{!Del}" rerender="pb1">

                        <apex:param name="rowToBeDeleted" value="{!e1.recCount}" assignTo="{!selectedRowIndex}"></apex:param>

                        </apex:commandButton>

                   </apex:column>

                   <apex:column >

                   <apex:facet name="header">Add</apex:facet>

                     <apex:commandButton value="+" action="{!Add}" rerender="pb1"/> 

                   </apex:column>

                </apex:pageblocktable>                

    </apex:pageBlock>        

</apex:pageblock>

</apex:form>

</apex:page>


Apex Class

public class MultiAdd{   
 
    public List<contact>lstAcct  = new List<contact>();
 
    public List<innerClass> lstInner{get;set;}
 
    public String selectedRowIndex{get;set;} 
 
    public Integer count = 1;
 
                
 
    public PageReference Save(){
 
        PageReference pr = new PageReference('/apex/MultiAdd');       
 
        for(Integer j = 0;j<lstInner.size();j++){
 
            lstAcct.add(lstInner[j].acct);
 
        }
 
        insert lstAcct;
 
        pr.setRedirect(True);
 
        return pr;
 
    }
 
         
 
    public void Add(){  
 
        count = count+1;
 
        addMoreRows();     
 
    }
 
     
 
    public void addMoreRows(){     
 
      innerClass objInnerClass = new innerClass(count);
 
      lstInner.add(objInnerClass);         
 
    }
 
     
 
    public void Del(){     
 
      lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
 
      count = count - 1;       
 
    }
 
                 
 
    public MultiAdd(ApexPages.StandardController ctlr){   
 
       lstInner = new List<innerClass>();
 
       addMoreRows();
 
       selectedRowIndex = '0';       
 
    }
 
         
 
    public class innerClass{              
 
        public String recCount{get;set;}               
 
        public Contact acct{get;set;}
 
        
 
        Public contact con{get;set;}
 
        public innerClass(Integer intCount){
 
            recCount = String.valueOf(intCount);                               
 
            con = new contact();           
 
        }
 
    }
 
}