You need to sign in to do that
Don't have an account?
VSK98
Unable to download the multiple workbooks in lightning
Hi All,
I am trying to download the multiple workbooks in lightning but it is not working.
Snippet code:
I am trying to download the multiple workbooks in lightning but it is not working.
Snippet code:
<aura:application > <table id="tbl1" class="table2excel"> <tr> <td>Product</td> <td>Price</td> <td>Available</td> <td>Count</td> </tr> <tr> <td>Bred</td> <td>1 </td> <td>2 </td> <td>3 </td> </tr> <tr> <td>Butter</td> <td>4 </td> <td>5 </td> <td >6 </td> </tr> </table> <hr></hr> <table id="tbl2" class="table2excel"> <tr> <td>Product</td> <td>Price</td> <td>Available</td> <td>Count</td> </tr> <tr> <td>Bred</td> <td>7 </td> <td>8 </td> <td>9 </td> </tr> <tr> <td>Butter</td> <td>14 </td> <td>15 </td> <td >16 </td> </tr> </table> <lightning:button label="Hit" onclick="{!c.tablesToExcel}"/> <!--<button onclick="tablesToExcel(['tbl1','tbl2'], ['ProductDay1','ProductDay2'], 'TestBook.xls', 'Excel')">Export to Excel</button>--> </aura:application>
({ tablesToExcel : function(component, event, helper) { var 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"><Author>Axel Richter</Author><Created>{created}</Created></DocumentProperties><Styles><Style ss:ID="Currency"><NumberFormat ss:Format="Currency"></NumberFormat></Style><Style ss:ID="Date"><NumberFormat ss:Format="Medium Date"></NumberFormat></Style></Styles>{worksheets}</Workbook>'; console.log('@@@@@'); var tmplWorksheetXML = '<Worksheet ss:Name="{nameWS}"><Table>{rows}</Table></Worksheet>'; var tmplCellXML = '<Cell{attributeStyleID}{attributeFormula}><Data ss:Type="{nameType}">{data}</Data></Cell>'; console.log('@@@@@'); var base64 = function(s) { console.log('base64'); return window.btoa(unescape(encodeURIComponent(s))) }; var format = function(s, c) { console.log('format'); return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }; var tables = document.createElement("table"); var table1 = document.createElement("table"); //return function(tables, wsnames, wbname, appname) { var ctx = ""; var workbookXML = ""; var worksheetsXML = ""; var rowsXML = ""; console.log('###!!@'); for (var i = 0; i < 2; i++) { if (!tables[i].nodeType) tables[i] = document.getElementById(tables[i]); for (var j = 0; j < tables[i].rows.length; j++) { rowsXML += '<Row>' for (var k = 0; k < tables[i].rows[j].cells.length; k++) { var dataType = tables[i].rows[j].cells[k].getAttribute("data-type"); var dataStyle = tables[i].rows[j].cells[k].getAttribute("data-style"); var dataValue = tables[i].rows[j].cells[k].getAttribute("data-value"); dataValue = (dataValue)?dataValue:tables[i].rows[j].cells[k].innerHTML; var dataFormula = tables[i].rows[j].cells[k].getAttribute("data-formula"); dataFormula = (dataFormula)?dataFormula:(appname=='Calc' && dataType=='DateTime')?dataValue:null; ctx = { attributeStyleID: (dataStyle=='Currency' || dataStyle=='Date')?' ss:StyleID="'+dataStyle+'"':'' , nameType: (dataType=='Number' || dataType=='DateTime' || dataType=='Boolean' || dataType=='Error')?dataType:'String' , data: (dataFormula)?'':dataValue , attributeFormula: (dataFormula)?' ss:Formula="'+dataFormula+'"':'' }; rowsXML += format(tmplCellXML, ctx); } rowsXML += '</Row>' } ctx = {rows: rowsXML, nameWS: wsnames[i] || 'Sheet' + i}; worksheetsXML += format(tmplWorksheetXML, ctx); rowsXML = ""; } ctx = {created: (new Date()).getTime(), worksheets: worksheetsXML}; workbookXML = format(tmplWorkbookXML, ctx); console.log(workbookXML); var link = document.createElement("A"); link.href = uri + base64(workbookXML); link.download = wbname || 'Workbook.xls'; link.target = '_blank'; document.body.appendChild(link); link.click(); document.body.removeChild(link); } // } })