'Save dataTable as excel xlsx format instead of xls

I have created a sample script below to save table as a xlsx format . Currently the script below is working as expected. the only differences is that it is exporting data as a xls format.

The actual script that was used is to download data is about 90k++ rows which will return browser to be Error code: Out of Memory .
I read up on xls documents , it states it only can support up to 60K+ worth of row. Im guessing that it might be due to this. However, i need some feedback as when i tried to use the same script as below to export as xlsx format . the file exported will be corrupted or unable to open

I have come into conlcusion thinking that xlsx format should do the trick but correct me if im wrong on this.

Sample reference script that was used for this poc is

<html>
<head>
    <script>
        var uri = 'data:application/vnd.ms-excel;base64,'
            , tmplWorkbookXML = '<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?><Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">'
                + '<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"></DocumentProperties>'
                + '{worksheets}</Workbook>'

            , tmplWorksheetXML = '<Worksheet ss:Name="{nameWS}"><Table>{rows}</Table></Worksheet>'
            , tmplCellXML = '<Cell><Data ss:Type="{nameType}">{data}</Data></Cell>'
            , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
            , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
        var ctx = "";
        var workbookXML = "";
        var worksheetsXML = "";
        var rowsXML = "";
        var dynamicColumnSize = "";

        let sampledata = ['farid hi say hello there ola', 'farid hi say hello there ola', 'farid hi say hello there ola', 'farid hi say hello there ola', 'farid hi say hello there ola', 'farid hi say hello there ola', 'farid hi say hello there ola', 'farid hi say hello there ola', 'farid hi say hello there ola', 'farid hi say hello there ola']




        rowsXML += '<Row >'
        counter = 0;
        for (let i = 0; i < sampledata.length; i++) {
            counter++;
            ctx = {
                nameType: 'String',
                data: sampledata[i]
            };
            rowsXML += format(tmplCellXML, ctx);
        }
        rowsXML += '</Row>'


        // Appending data to sheet
               if (rowsXML != '') {
            ctx = { rows: rowsXML, nameWS: 'FaridSheet' };
            worksheetsXML += format(tmplWorksheetXML, ctx);
            rowsXML = "";
        }

        const saveExcel = () => {
            console.log(tmplWorkbookXML)
            ctx = { created: "DOWNLOAD REPORT", worksheets: worksheetsXML };
            workbookXML = format(tmplWorkbookXML, ctx);
            var link = document.createElement("A");
            link.href = uri + base64(workbookXML);
            link.download = "faridtest" + ".xls";
            link.target = '_blank';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        }

    </script>
</head>

<body>
    <button onClick="saveExcel()">HI download</button>

</html>

Above script will export as .xls format , when i tried to update SaveExcel() from link.download = "faridtest" + ".xls"; to link.download = "faridtest" + ".xlsx";

download will work and file will be exported to my download folder. However but it wont open the file as it will prompt error msg as shown in image below

Should URI declaration be updated ? Any recommended approach

**Please note that this should be without any third library javascript.

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source