You need to sign in to do that
Don't have an account?
Yogesh Rashinkar
Does lightning component javascript controller supports to create xlsx file?
In below code i am creating XLS file using lightning javascript controller but it is ending up with creating an xls file how i can create XLSX file?? in the same way if i create xlsx file, file is not opening..
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:application/vnd.ms-excel;charset=utf-8' + ',' + encodeURIComponent(csv);
hiddenElement.download = 'Report.xls';
hiddenElement.style="border: 1.5pt";
document.body.appendChild(hiddenElement); // Required for FireFox browser
hiddenElement.click();
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:application/vnd.ms-excel;charset=utf-8' + ',' + encodeURIComponent(csv);
hiddenElement.download = 'Report.xls';
hiddenElement.style="border: 1.5pt";
document.body.appendChild(hiddenElement); // Required for FireFox browser
hiddenElement.click();
Try this
---------Component--------
<aura:component >
<table id="table" border="1">
<tr>
<td>
Foo
</td>
<td>
Bar
</td>
</tr>
</table>
<a aura:id="downloadLink" onclick="{!c.myAction}">Export to excel</a>
</aura:component>
----------Controller.js----------
({
myAction : function(component, event, helper) {
var elem1 = component.find("downloadLink");
var elem = elem1.getElement();
var table = document.getElementById("table");
var html = table.outerHTML;
var url = 'data:application/vnd.ms-excel,' + escape(html); // Set your html table into url
elem.setAttribute("href", url);
elem.setAttribute("download", "export.xls"); // Choose the file name
return false;
}
})
-------Application-------
<aura:application extends="force:slds" >
<c:ExcelFile/>
</aura:application>
Greetings to you!
I have read your query and I find out you have not set the target in the variable hiddenElement.
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
hiddenElement.target = '_self'; //
hiddenElement.download = 'ExportData.csv'; // CSV file Name* you can change it.[only name not .csv]
document.body.appendChild(hiddenElement); // Required for FireFox browser
hiddenElement.click();
If it doesn't resolve your problem please go through the below-given link this link has a very good example of your problem.
Link:- http://sfdcmonkey.com/2017/04/19/download-csv-file-data-salesforce-lightning-component/
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
lightning component javascript controller supports to create xlsx file, follow below link it may helpful for you:
https://salesforce.stackexchange.com/questions/186448/using-xslx-js-library-on-lightning-component
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi