Generate excel of opportunity data using VF Page.

Nowadays excel files are very common and easy to share with other people. So there is a quite high demand in salesforce as well, as we all know data loader in salesforce uses csv files to import the files  but we need a functionality that will generate an excel file using the opportunity object record data which gives us the option to export the excel as well.

Create a VF page Name : ' ExportExcel.vfp '

				
					<apex:page standardController="Opportunity" Extensions="ExportExcel" readOnly="true" standardStylesheets="false" contentType="application/vnd.ms-excel#Data_{!opportunity.Name}_{!TODAY()}.xls">
    {!xmlheader}
    <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
              xmlns:o="urn:schemas-microsoft-com:office:office"
              xmlns:x="urn:schemas-microsoft-com:office:excel"
              xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
              xmlns:html="http://www.w3.org/TR/REC-html40">
        <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
            <Author></Author>
            <LastAuthor></LastAuthor>
            <Company></Company>
            <Version>1</Version>
        </DocumentProperties>
        
        <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
            <WindowHeight>16000</WindowHeight>
            <WindowWidth>20480</WindowWidth>
            <WindowTopX>400</WindowTopX>
            <WindowTopY>400</WindowTopY>
            <ProtectStructure>False</ProtectStructure>
            <ProtectWindows>False</ProtectWindows>
        </ExcelWorkbook>
        <Styles>
            <Style ss:ID="Default" ss:Name="Normal">
                <Alignment ss:Vertical="Bottom"/>
                <Borders/>
                <Font/>
                <Interior/>
                <NumberFormat/>
                <Protection/>
            </Style>
            <Style ss:ID="s01">
                <Alignment ss:Vertical="Bottom"/>
                <Font ss:Size="12" ss:Bold="1"/>
                <Interior ss:Color="#e1ebe4" ss:Pattern="Solid"/>
            </Style>
            
        </Styles>
        
        <Worksheet ss:Name="Opportunity Data">
            <Table ss:ExpandedColumnCount="50" ss:ExpandedRowCount="35" x:FullColumns="1"
                   x:FullRows="1" ss:DefaultColumnWidth="85">

                <Column ss:AutoFitWidth="0" ss:Width="124"/>
                <Column ss:AutoFitWidth="0" ss:Width="124"/>
                <Column ss:AutoFitWidth="0" ss:Width="124"/>
                <Column ss:AutoFitWidth="0" ss:Width="124"/>
                <Column ss:AutoFitWidth="0" ss:Width="124"/>
                <Row ss:Height="16">
                    <Cell ss:StyleID="s01"><Data ss:Type="String">Name</Data></Cell>
                    <Cell ss:StyleID="s01"><Data ss:Type="String">Amount </Data></Cell>
                    <Cell ss:StyleID="s01"><Data ss:Type="String">StageName</Data></Cell>
                    <Cell ss:StyleID="s01"><Data ss:Type="String">CloseDate</Data></Cell>
                    
                </Row>
				<apex:repeat value="{!oppWrapper}" var="opp">
                    <Row ss:Height="16">
                        <Cell ><Data ss:Type="String">{!opp.Name}</Data></Cell>
                        <Cell ><Data ss:Type="Number">{!opp.Amount}</Data></Cell>
                        <Cell ><Data ss:Type="String">{!opp.StageName }</Data></Cell>
                        <Cell ><Data ss:Type="String">
                            	  <apex:outputText value="{0, date, MM'/'dd'/'yyyy}">
                                    <apex:param value="{!opp.CloseDate}" />
                                  </apex:outputText> 
                               </Data>
                        </Cell>
                    </Row>
                    </apex:repeat>
                
            </Table>
            <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
                <PageLayoutZoom>0</PageLayoutZoom>
                <Selected/>
                <FreezePanes/>
                <FrozenNoSplit/>
                <SplitHorizontal>1</SplitHorizontal>
                <TopRowBottomPane>1</TopRowBottomPane>
                <ActivePane>2</ActivePane>
                <Panes>
                    <Pane>
                        <Number>3</Number>
                    </Pane>
                </Panes>
                <ProtectObjects>False</ProtectObjects>
                <ProtectScenarios>False</ProtectScenarios>
            </WorksheetOptions>
        </Worksheet>
    </Workbook>
</apex:page>
				
			

Create an Apex class Name : ' ExportExcel'

				
					public class ExportExcel {
    public List<OpportunityWrapper> oppWrapper{get; set;}
	public String xmlheader {get;set;}
    public String endfile{get;set;}
    private ApexPages.StandardController controller;
    public ExportExcel(ApexPages.StandardController controller){
        this.controller = controller;
		xmlheader ='<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?>';
        endfile = '</Workbook>';
    
        oppWrapper = new List<OpportunityWrapper>();
 
        try {
                for(Opportunity oppty : [SELECT Id, Name, Amount, StageName , CloseDate FROM Opportunity where Id =: controller.getId()]){
                                                        OpportunityWrapper oppWrp = new OpportunityWrapper();
														oppWrp.Name = oppty.Name;
														oppWrp.Amount = Oppty.Amount;
														oppWrp.StageName = Oppty.StageName;
														oppWrp.CloseDate = Oppty.CloseDate;
														oppWrapper.add(oppWrp);
                                                        }
        } catch(Exception ex){
            System.debug('Error : ' + ex.getMessage() +' '+ ex.getCause() + ' '+  ex.getLineNumber());
        }
	}		
    
    public class OpportunityWrapper{
        public String Name{get; set;}
        public Decimal Amount{get; set;}
        public String StageName{get; set;}
        public Date CloseDate {get; set;}
    }
}
				
			

Create a  button Name : ‘ Opportunity  Download  ‘

Step 1 :  Go to Opportunity object.  Click ‘ Button Links and Actions ‘

Step 2 : Enter Label ‘ Download Opportunity  ‘.

Step 3 : Select Display Type : ‘ Detail Page Button ‘ .

Step 4 : Select Behavior : ‘ Display in existing window with sidebar

Step 5 : Select Content Source : ‘ Visualforce Page

Step 6 : Click on Save .

 

Add Download Opportunity in Salesforce Mobile and Lightning Experience

Download opportunity

Leave a Comment