Download Salesforce Visualforce Page in Excel Format

Download Salesforce Visualforce Page in Excel Format

contentType=”application/vnd.ms-excel#AccountFile.xls” can be used to download the Visualforce Page in Excel Format.

Sample Code:

Apex Controller:

public class AccountExcelFileController {
    
    public String xlsHeaderInfo {
        get {
            String strHeader = '<?xml version="1.0"?>' +
            '<?mso-application progid="Excel.Sheet"?>';
            return strHeader;
        }
    }
    
    public AccountExcelFileController(
        ApexPages.StandardController stdController
    ) {
    }
    
}

Visualforce Page for Quick Action:

<apex:page 
           standardController="Account"
           extensions="AccountExcelFileController"
           showHeader="false" 
           standardStylesheets="false" 
           showQuickActionVfHeader="false"
           cache="true">
    <center>
        <h1>
            Downloading the File!!!
        </h1>
    </center>
    <script 
    	type='text/javascript' 
        src='/canvas/sdk/js/publisher.js'>
    </script>
    <script>
        window.onload=function() {  
            window.open(
                '/apex/AccountExcelFileContent?Id={!Account.Id}'
            );
            console.log(
                'Closing the VF Quick Action'
            );
            Sfdc.canvas.publisher.publish(
                { 
                    name: "publisher.close", 
                    payload:{ 
                        refresh: "true" 
                    }
                }
            );
        };
    </script>
</apex:page>

Visualforce Page for Excel Content:

<apex:page standardController="Account"
           extensions="AccountExcelFileController"
           showHeader="false" 
           standardStylesheets="false" 
           contentType="application/vnd.ms-excel#{!Account.Name} Test {!TODAY()}.xls" 
           showQuickActionVfHeader="false"
           cache="true">
    <apex:outputText value="{!xlsHeaderInfo}"/>
    <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">
        <Styles>
            <Style ss:ID="s1">
                <Alignment ss:Horizontal="Center"/>
                <Borders/>
                <Font ss:Bold="1"/>
                <Interior/>
                <NumberFormat/>
                <Protection/>
            </Style>
        </Styles>
        <Worksheet ss:Name="Account Information">
            <Table x:FullColumns="1" x:FullRows="1">
                <Column ss:Width="200"/>
                <Column ss:Width="200"/>
                <Row>
                    <Cell ss:StyleID="s1">
                        <Data ss:Type="String" >Name</Data>
                    </Cell>
                    <Cell ss:StyleID="s1">
                        <Data ss:Type="String" >Industry</Data>
                    </Cell>
                </Row>
                <Row>
                    <Cell>
                        <Data ss:Type="String">{!Account.Name}</Data>
                    </Cell>
                    <Cell>
                        <Data ss:Type="String">{!Account.Industry}</Data>
                    </Cell>
                </Row>
            </Table>
        </Worksheet>
        <Worksheet ss:Name="Contacts Information">
            <Table x:FullColumns="1" x:FullRows="1">
                <Column ss:Width="200"/>
                <Column ss:Width="200"/>
                <Column ss:Width="200"/>
                <Row>
                    <Cell ss:StyleID="s1">
                        <Data ss:Type="String" >First Name</Data>
                    </Cell>
                    <Cell ss:StyleID="s1">
                        <Data ss:Type="String" >Last Name</Data>
                    </Cell>
                    <Cell ss:StyleID="s1">
                        <Data ss:Type="String" >Email</Data>
                    </Cell>
                </Row>
                <apex:repeat value="{!Account.Contacts}" var="con">
                    <Row>
                        <Cell>
                            <Data ss:Type="String">{!con.FirstName}</Data>
                        </Cell>
                        <Cell>
                            <Data ss:Type="String">{!con.LastName}</Data>
                        </Cell>
                        <Cell>
                            <Data ss:Type="String">{!con.Email}</Data>
                        </Cell>
                    </Row>
                </apex:repeat>
            </Table>
        </Worksheet>
    </Workbook>
</apex:page>

Create a custom Quick Action with the Action Type as “Custom Visualforce” and add it to the page layout.

Leave a Reply