I have been using a lightning component, on clicking download button. New tab is opened which hit to the vf page url which downloads an excel. Can we use some other steps of redirection so that the UI remains same and excel gets downloaded.

In VF page, we have wrapper which gets filled by apex class. We dont need to show data in UI. Just one download button to get data.


1 Answer 1


This solution is implemented using Aura framework. I have posted a similar solution using LWC as well.

This is an interesting scenario. In past, I have used download.js to trigger file download from JavaScript.

So I have tried to use the same concept here to download the excel file upon clicking of a button in Lightning UI.

The main idea is to convert the Excel into Base64 encoded String and pass it to the download method of download.js. Which will show the file download window upon the same screen.

Here are my code and screecap in action.

Lightning Component Markup

<aura:component controller="ExcelController" implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId">
    <aura:attribute name="recordId" type="Id" />
        <ltng:require scripts="{!$Resource.downloadjs}"/>
        <lightning:button variant="brand" label="Download Excel Report" onclick="{! c.downloadExcelFile }" />

Lightning Component Controller

    downloadExcelFile : function(component) { 
        var action = component.get("c.getExcelFileAsBase64String");
        var self = this;
            contactId: component.get("v.recordId")
        action.setCallback(this, function(action){
            var state = action.getState();
            if(state == 'SUCCESS') {
                var strFile = "data:application/excel;base64,"+action.getReturnValue();
                download(strFile, "myexcelfile.xls", "application/excel");

Apex Class ExcelController

public class ExcelController {
    public static String getExcelFileAsBase64String(Id contactId) {
        PageReference excelPage = Page.excelfilepage;
        Blob excelBlob = excelPage.getContent();
        String base64Excel = EncodingUtil.base64Encode(excelBlob);
        return base64Excel;

VF Page to generate Excel

<apex:page controller="contactquery" contentType="application/vnd.ms-excel">
    <apex:pageBlock title="Export Results" >
        <apex:pageBlockTable value="{!cs}" var="contact">
            <apex:column value="{!contact.ID}"/>
            <apex:column value="{!contact.Name}"/>

Apex Class contactquery used for excel generation

public class contactquery{
    public List<Contact> cs{get; set;}
    public contactquery(){
        String cid = ApexPages.currentPage().getParameters().get('cid');
        cs = new List<Contact>();
        for (Contact c : [Select id, Name from Contact WHERE Id =:cid]){       
            System.debug('Inside for '+c);

Screencap in action

enter image description here

  • Thanks @saroj Bera, for this helpful concept. It has worked for me. Commented Sep 5, 2018 at 8:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .