Error while exporting excel : "The file format and Extension don’t match”

Tapish Sharma1
Kilo Sage

Hi Folks, 

I have a UI page report where I am exporting the report to excel. But the  excel which is generated throws an error when opening saying - 'The file format and Extension dont match'. Any idea how to get rid of this error ? 

Below is the client script I am using to generate the excel - 

function fnExcelReport(file_name) {

 

var tab_text = "<table border='2px'><tr bgcolor='#D3D3D3'>";


var fname = document.getElementById('sysparm_number').value;


var textRange;

 

var j = 0;

 

tab = document.getElementById('one'); // id of table

 

 

 

for (j = 0; j < tab.rows.length; j++) {

 

tab_text = tab_text + tab.rows[j].innerHTML + "</tr>";

 

//tab_text=tab_text+"</tr>";

 

tab_text = tab_text.replace(/Remove/i, "");

 

}

 

 

 

tab_text = tab_text + "</table>";

 

tab_text = tab_text.replace(/<A[^>]*>|<\/A>/g, ""); //remove if u want links in your table

 

tab_text = tab_text.replace(/<img[^>]*>/gi, ""); // remove if u want images in your table

 

tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params

 

//tab_text = tab_text.replace(/Remove/i, ""); // reomves theremove button label.

 

 

 

var ua = window.navigator.userAgent;

 

var msie = ua.indexOf("MSIE ");

 

if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer

 

{

 

txtArea1.document.open("txt/html", "replace");

 

txtArea1.document.write(tab_text);

 

txtArea1.document.close();

 

txtArea1.focus();

 

sa = txtArea1.document.execCommand("SaveAs", true, "test.xlsx");

 

} else //other browser not tested on IE 11


{
//
//sa = window.open('data&colon;application/vnd.ms-excel,' + encodeURIComponent(tab_text));
sa = document.createElement('a');
//getting data from our div that contains the HTML table
var data_type = 'data&colon;application/vnd.ms-excel';
sa.href = data_type + ', ' + encodeURIComponent(tab_text);
//setting the file name
sa.download = fname+ '.xls';
//triggering the function
sa.click();
}

 

 

 

return (sa);

'

2 REPLIES 2

Ratnakar7
Mega Sage
Mega Sage

Hi @Tapish Sharma1 ,

 

To resolve this issue, you can try with replacing below line 

sa.download = fname + '.xls';

with 

sa.download = fname + '.xlsx'; // or sa.download = fname + '.html'; 

 

Thanks,

Ratnakar

 

Hi Ratnakar, 
The excel is getting downloaded as .html I guess , if I change the name to .html then it gives an html file but I dont want that