UNPKG

@testqueryparkui/qp-es-ui

Version:
66 lines (60 loc) 3.02 kB
const dataToExcel = async (data) => { //console.log('HERE') let success = false 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"><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>' , tmplWorksheetXML = '<Worksheet ss:Name="{nameWS}"><Table>{rows}</Table></Worksheet>' , tmplCellXML = '<Cell{attributeStyleID}{attributeFormula}><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 = ""; for(let i=0; i<data.length; i++){ //if (!tables[i].nodeType) tables[i] = document.getElementById(tables[i]); for (var j = 0; j < data[i].value.length; j++) { rowsXML += '<Row>' for (let k in data[i].value[j]) { //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"); let dataValue = data[i].value[j][k]; // var dataFormula = tables[i].rows[j].cells[k].getAttribute("data-formula"); //dataFormula = (dataFormula)?dataFormula:(appname=='Calc' && dataType=='DateTime')?dataValue:null; ctx = { attributeStyleID: '' , nameType: 'String' , data:dataValue , attributeFormula: '' }; rowsXML += format(tmplCellXML, ctx); } rowsXML += '</Row>' } ctx = {rows: rowsXML, nameWS: data[i].key}; 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 = 'querypark.xls'; link.target = '_blank'; document.body.appendChild(link); link.click(); document.body.removeChild(link); if(link) { success = true return success } } export default dataToExcel