Hi All,
I have the following code in my .ts file which is creating excel export file but while opening the file in MS EXCEL, it is not opening and showing error as file has been currupted or bad file extension.
- ExportExcel(objArray: any, filename: string) {
- var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
- var str = '<table>';
- var row = "<tr>";
-
- for (var index in objArray[0]) {
- row += '<th>' + index + '</th>'
- }
- row = row.slice(0, -5);
- str += row + '</tr>';
-
- for (var i = 0; i < array.length; i++) {
- var line = '<tr>';
- for (var index in array[i]) {
- line += '<td>' + array[i][index] + '</td>';
- }
- str += line + '</tr>';
- }
-
- str += '</table>';
- var a = document.createElement("a");
- a.setAttribute('style', 'display:none;');
- document.body.appendChild(a);
- var blob = new Blob([str], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
- var url = window.URL.createObjectURL(blob);
- a.href = url;
- a.download = filename + '.xlsx';
- a.click();
-
- str = '';
- row = '';
- }
Any help on this would be highly appriciated.