function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JustAGirlyGeekJustAGirlyGeek 

Exporting to Excel in Lightning

In Classic, you can create a simple Visualforce page to export records to Excel using contentType. For example:
<apex:page StandardController="Opportunity" contentType="application/vnd.ms-excel#export_{!NOW()}.xls">
    <apex:pageBlock title="Export Results">
        <apex:pageBlockTable value="{!Opportunity.OpportunityLineItems}" var="item">
            <apex:column value="{!item.PricebookEntry.Name}"/>        
            <apex:column value="{!item.PricebookEntry.ProductCode}"/>    
            <apex:column value="{!item.Description}"/>
            <apex:column value="{!item.ServiceDate}"/>
            <apex:column value="{!item.Quantity}"/>
            <apex:column value="{!item.Sales_Price__c}"/>            
            <apex:column value="{!item.TotalPrice}"/> 
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>


I need this to work in Lightning but I am hitting a roadblock with getting this to work right. If I use a custom button to call the VF page, the export works, but leaves me with a blank screen instead of taking me back to the record where I clicked the button. If I use a Quick Action to call the VF page, it still works, but now leaves me with a modal and the only option is to click Cancel.

What are my other options here? I am not a developer so my skills are a bit limited. TIA!
Alain CabonAlain Cabon
Hi JustAGirlyGeek,

My strange solution seems to work but the trick is used just for closing the "Quick Action" popup ( $A.get("e.force:closeQuickAction")).
<aura:component implements="flexipage:availableForAllPageTypes,force:hasRecordId,force:lightningQuickAction" access="global" >
   <aura:handler name="init" value="{!this}" action="{!c.dismissIt}" />
    <aura:attribute name="vfHost" type="String" default="your_domain.my.salesforce.com"/>
    <!-- The Visualforce page call -->
    <iframe aura:id="vfFrame" src="{!'https://' + v.vfHost + '/apex/opportunityxls?id=' + v.recordId}"/>
</aura:component>

Controller: the toast is used for nice messages in LEX.
({
    dismissIt : function(component, event, helper) {            
        var action = component.get("c.getFoo");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if(state === "SUCCESS") {  
                var resultsToast = $A.get("e.force:showToast");
                resultsToast.setParams({
                    "title": "Success!",
                    "message": "Excel file exported."
                });
                resultsToast.fire();      
                var wasDismissed = $A.get("e.force:closeQuickAction");
                wasDismissed.fire();
            } else {
                console.log('Problem response state: ' + state);
            }
        });
        $A.enqueueAction(action);   
    },
    getFoo: function(component, event, helper) {  
    }
})

opportunityxls​.vfp (your code):
<apex:page StandardController="Opportunity" contentType="application/vnd.ms-excel#export_{!NOW()}.xls">
    <apex:pageBlock title="Export Results">
        <apex:pageBlockTable value="{!Opportunity.OpportunityLineItems}" var="item">
            <apex:column value="{!item.PricebookEntry.Name}"/>        
            <apex:column value="{!item.PricebookEntry.ProductCode}"/>    
            <apex:column value="{!item.Description}"/>
            <apex:column value="{!item.ServiceDate}"/>
            <apex:column value="{!item.Quantity}"/>
            <apex:column value="{!item.Sales_Price__c}"/>            
            <apex:column value="{!item.TotalPrice}"/> 
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Regards
JustAGirlyGeekJustAGirlyGeek
Alain,
Thank you! I will give this a try.
Alain CabonAlain Cabon
Ok. The trick seems not to work at the first attempt sometimes (refresh?) but afterwards, I have got an excel export at each try.
It will be interesting to export a big list in the excel files (not tested).

If you find another alternative, could you also post it here? (alternative for the iframe for example).

Sending the Message in the Lightning Component​: also uses the iframe.
https://developer.salesforce.com/blogs/developer-relations/2017/01/lightning-visualforce-communication.html