-1

I need to export data in '.XLSX' file for Chrome browser below code is working fine for IE.

But code in else condition is creating download.xls file Chrome browser

sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  

Below is the code used on Button Click :

function fnExcelReport()
{
    var tab_text="<table border='2px'><tr bgcolor='#87AFC6'>";
    var textRange; var j=0;
    tab = document.getElementById('headerTable'); // 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+"</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

    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,"ABCD.xlsx");
    }  
    else                 //other browser not tested on IE 11
        sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  

    return (sa);
}

Please help me on this, thanks in advance.

Please suggest me solution with above code , I am new to javascript and unable to implement the solution in suggested link .

I have also used below code to create .XLSX file, Code is creating file but file is blank and showing an error message: "The file Format and extension do not match.The file could be corrupted or unsafe......Do u want to opent it anyway"

var a = document.createElement('a');
var data_type = 'data:application/vnd.ms-excel';
var table_div = tab_text;    //Your tab_text   
var table_html = table_div.replace(/ /g, '%20');
a.href = data_type + ', ' + table_html;
//setting the file name
a.download = 'exported_table.xlsx';
//triggering the function
a.click();
Neha
  • 57
  • 3
  • 15
  • Possible duplicate of [how to generate Excel through Javascript](https://stackoverflow.com/questions/333537/how-to-generate-excel-through-javascript) – Liam Aug 08 '18 at 12:41
  • Please suggest me solution with above code , I am new to javascript and unable to implement the solution in suggested link . – Neha Aug 08 '18 at 17:05

1 Answers1

0

You can do like this:

window.saveAs(blob,filename);

function fnExcelReport(id, name) {
var tab_text = '<html xmlns:x="urn:schemas-microsoft-com:office:excel">';
tab_text = tab_text + '<head><xml><x:ExcelWorkbook><x:ExcelWorksheets> 
<x:ExcelWorksheet>';
tab_text = tab_text + '<x:Name>Test Sheet</x:Name>';
tab_text = tab_text + '<x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions> 
</x:ExcelWorksheet>';
tab_text = tab_text + '</x:ExcelWorksheets></x:ExcelWorkbook></xml></head><body>';
tab_text = tab_text + "<table border='1px'>";
var exportTable = $('#' + id).clone();
exportTable.find('input').each(function (index, elem) { $(elem).remove(); });
tab_text = tab_text + exportTable.html();
tab_text = tab_text + '</table></body></html>';
var fileName = name + '_' + parseInt(Math.random() * 10000000000) + '.xls';

//Save the file
var blob = new Blob([tab_text], { type: "application/vnd.ms-excel;charset=utf-8" })
window.saveAs(blob, fileName);
}`
Julio Amorim
  • 43
  • 1
  • 12