Lightning · Salesforce


This Post explains how to download the excel file that is created as VF Page inside LWC Component.

  1. Download the js file named download.js from the link and add it as a static resource.
  2. Create a Visual force Page for the file that is to be generated as excel.

<apex:page controller="TestExcelExport" contentType="application/">
     <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="">
            <Style ss:ID="Default" ss:Name="Normal">
                <Alignment ss:Vertical="Bottom" ss:Horizontal="Left" ss:WrapText="1"/><Borders/><Font/><Interior/><NumberFormat/><Protection/>
            <Style ss:ID="s0">
                <Alignment ss:Vertical="Bottom" ss:Horizontal="Left" ss:WrapText="0"/>
        <Worksheet ss:Name="Deal Eval Output">
            <Table x:FullColumns="1" x:FullRows="1">
                    <Cell ss:StyleID="s0">
                        <Data ss:Type="String">Opportunity</Data>
                    <Cell ss:StyleID="s0">
                        <Data ss:Type="String">Test Opportunity</Data>
                    <Cell ss:StyleID="s0">
                        <Data ss:Type="String">Opportunity Owner</Data>
                    <Cell ss:StyleID="s0">
                        <Data ss:Type="String">Test Developer</Data>
                    <Cell ss:StyleID="s0">
                        <Data ss:Type="String">Currency</Data>
                    <Cell ss:StyleID="s0">
                        <Data ss:Type="String">USD</Data>

3. Create a lwc Component from where the file has has be downloaded.


   		 <lightning-button label="Export to Excel" title="Export to Excel" onclick={downloadExcelFile}


import {LightningElement, api, wire, track } from 'lwc';
import getExcelFileAsBase64String from "@salesforce/apex/GetExcelFile.getExcelFileAsBase64String";
import DownloadExcel from "@salesforce/resourceUrl/DownloadExcel";

export default class DownloadExcelComponent extends LightningElement {
//Loading the static resource js to download the excel
    loadScript(this, DownloadExcel)
    .then(() => {
      console.log("loaded DownloadExcel");
    .catch(error => {
      console.log('Error in loading script', JSON.stringify(error));
      this.error = error;
   //Downloading the Excel File
          .then(result => {
              var strFile = "data:application/excel;base64,"+ result;
              download(strFile, "TestFile.xls", "application/excel");          
          .catch(error => {
              new ShowToastEvent({
                  title: "Error!",
                  message: "Error occured while downloading Excel-"+ error,
                  variant: "Error",
                  mode: 'sticky'

4. Create a controller for getting the contents of visual force page


public with sharing class GetExcelFile {
    public static String getExcelFileAsBase64String() {
        PageReference excelPage = Page.TestExcelExport;
        Blob excelBlob = excelPage.getContent();
        String base64Excel = EncodingUtil.base64Encode(excelBlob);
        return base64Excel;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s