Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

javascript - Browser getting crashed when trying to export to excel more records using jQuery and XLSX.min.js

I am using sheet.js/xlsx.js files to export data to excel/csv.The code is working fine upto 120K records and its getting crashed if the number of records are more than 120k records. I am constructing BLOB and I am getting error in the console as "Paused before potential out-of-memory crash"

Code snippet to convert Json Data to excel using xslx.js

   function GenerateExcelData(excelData) {
            exportResult = [];
            let columns = [];
            $("#ddlFilterDataSelectedlist > option").each(function () {
                columns.push(this.value);
            });
            let data = [];
            for (let i = 0; i < excelData.length; i++) {
                let result = excelData[i];
                var jsonData = {};
                for (let column = 0; column < columns.length; column++) {
                    var dataVal = result[columns[column]];
                    if (dataVal && dataVal.toString().indexOf("/Date(") > -1)
                        dataVal = ConvertJsonDateString(dataVal);
                    jsonData[columns[column]] = dataVal;
                }
                data.push(jsonData);
            }
      //using xlsx.js function to convert json data to excel
            var wb = XLSX.utils.book_new();
            const ws = XLSX.utils.json_to_sheet(data);
            XLSX.utils.book_append_sheet(wb, ws, "Sample");
            var wbout = XLSX.write(wb, { bookType: 'xlsx', type: 'binary' });

                function s2ab(s) {
                    var buf = new ArrayBuffer(s.length);
                    var view = new Uint8Array(buf);
                    for (var i = 0; i < s.length; i++) view[i] = s.charCodeAt(i) & 0xFF;
                    return buf;
                  }
                   exportBlob = new Blob([s2ab(wbout)], { type: "application/octet-stream" });
                   saveAs(new Blob([s2ab(wbout)], { type: "application/octet-stream" }), getFileName() + '.xlsx');
           }

As the data is getting stored in BLOB which is using internal memory and when data is huge, browser is getting crashed.

I tried to do add divide records and appending to blob, but even this did not work as file is unable to view in excel. code snippet to append to blob

 exportBlob = new Blob([s2ab(wbout)], { type: "application/octet-stream" })
  exportBlob = new Blob([exportBlob,s2ab(wbout)], { type: "application/octet-stream" });

Please let me know if there is solution.

question from:https://stackoverflow.com/questions/65623300/browser-getting-crashed-when-trying-to-export-to-excel-more-records-using-jquery

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...