Created
October 31, 2017 07:19
-
-
Save earthchie/e5bcc9820951fb0628ee07089ac1eeba to your computer and use it in GitHub Desktop.
saveExcel function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function saveExcel(data, filename){ | |
var datenum = function (v, date1904) { | |
if(date1904) v+=1462; | |
var epoch = Date.parse(v); | |
return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000); | |
}, | |
sheet_from_array_of_arrays = function (data, opts) { | |
var ws = {}; | |
var range = {s: {c:10000000, r:10000000}, e: {c:0, r:0 }}; | |
for(var R = 0; R != data.length; ++R) { | |
for(var C = 0; C != data[R].length; ++C) { | |
if(range.s.r > R) range.s.r = R; | |
if(range.s.c > C) range.s.c = C; | |
if(range.e.r < R) range.e.r = R; | |
if(range.e.c < C) range.e.c = C; | |
var cell = {v: data[R][C] }; | |
if(cell.v == null) continue; | |
var cell_ref = XLSX.utils.encode_cell({c:C,r:R}); | |
if(typeof cell.v === 'number') cell.t = 'n'; | |
else if(typeof cell.v === 'boolean') cell.t = 'b'; | |
else if(cell.v instanceof Date) { | |
cell.t = 'n'; | |
cell.z = XLSX.SSF._table[14]; | |
cell.v = datenum(cell.v); | |
} | |
else cell.t = 's'; | |
ws[cell_ref] = cell; | |
} | |
} | |
if(range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range); | |
return ws; | |
}, | |
Workbook = function () { | |
if(!(this instanceof Workbook)) return new Workbook(); | |
this.SheetNames = []; | |
this.Sheets = {}; | |
}, | |
s2ab = function (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; | |
}, | |
obj2array = function(data){ | |
if(!(data[0] instanceof Array)){ | |
var keys = Object.keys(data[0]), | |
arrdata = []; | |
data.forEach(function(obj){ | |
if(Object.keys(obj).length > keys.length){ | |
keys = Object.keys(obj); | |
} | |
}); | |
data.forEach(function(obj){ | |
var arr = []; | |
keys.forEach(function(key){ | |
arr.push(obj[key]); | |
}) | |
//console.log(arr) | |
arrdata.push(arr); | |
}) | |
//console.log(keys, data); | |
data = [keys].concat(arrdata); | |
} | |
//console.log(data); | |
return data; | |
}, | |
wb = new Workbook(), | |
array_buffer, | |
file; | |
if(typeof data === 'string' && ~data.indexOf('<table')){ | |
node = document.createElement('div'); | |
node.innerHTML = data; | |
wb.SheetNames.push('Sheet1'); | |
wb.Sheets['Sheet1'] = XLSX.utils.table_to_book(node.getElementsByTagName('table')[0]).Sheets['Sheet1']; | |
} else if(data instanceof Array){ | |
wb.SheetNames.push('Sheet1'); | |
wb.Sheets['Sheet1'] = sheet_from_array_of_arrays(obj2array(data)); | |
}else{ | |
Object.keys(data).forEach(function(sheet_name){ | |
var sheet_data = data[sheet_name]; | |
if(typeof sheet_data === 'string' && ~sheet_data.indexOf('<table')){ | |
node = document.createElement('div'); | |
node.innerHTML = sheet_data; | |
wb.SheetNames.push(sheet_name); | |
wb.Sheets[sheet_name] = XLSX.utils.table_to_book(node.getElementsByTagName('table')[0]).Sheets['Sheet1']; | |
}else{ | |
wb.SheetNames.push(sheet_name); | |
wb.Sheets[sheet_name] = sheet_from_array_of_arrays(obj2array(sheet_data)); | |
} | |
}); | |
} | |
array_buffer = s2ab(XLSX.write(wb, {bookType:'xlsx', bookSST:true, type: 'binary'})); | |
file = new Blob([array_buffer],{type:'application/octet-stream'}); | |
saveAs(file, (filename||'data')+'.xlsx') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
html
javascript