Not able to export data into xlsx format using jquery

Copper Contributor

Hi,

I have written below script to generate data into xlsx format.

But I am getting message as below when I am opening xlsx excel sheet to view the data

'Excel cannot open the file abc.xlsx because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file'.

function exportToExcel(){
var downloadurl;
var dataFileType = 'application/vnd.ms-excel';
var tableSelect = document.getElementById('table2excel');
var tableHTMLData = tableSelect.outerHTML.replace(/ /g, '%20');
// Specify file name
filename = 'abc.xlsx';
// Create download link element
downloadurl = document.createElement("a");
document.body.appendChild(downloadurl);
if(navigator.msSaveOrOpenBlob){
var blob = new Blob(['\ufeff', tableHTMLData], {
type: dataFileType
});
navigator.msSaveOrOpenBlob( blob, filename);
}else{
// Create a link to the file
downloadurl.href = 'data:' + dataFileType + ', ' + tableHTMLData;
// Setting the file name
downloadurl.download = filename;
//triggering the function
downloadurl.click();
}
}


Regards,
Sudheer

 

0 Replies