-
Notifications
You must be signed in to change notification settings - Fork 5
/
jsontosheet.html
112 lines (94 loc) · 2.89 KB
/
jsontosheet.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!-- saved from url=(0039)http://sheetjs.com/demos/writexlsx.html -->
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<script type="text/javascript" src="./src/test.js"></script>
<script type="text/javascript" src="./src/Blob.js"></script>
<script type="text/javascript" src="./src/FileSaver.js"></script>
<script>
function datenum(v, date1904) {
if(date1904) v+=1462;
var epoch = Date.parse(v);
return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
}
function json_to_sheet(js/*:Array<any>*/, opts)/*:Worksheet*/ {
var o = opts || {};
var ws = ({}/*:any*/);
var range/*:Range*/ = ({s: {c:0, r:0}, e: {c:0, r:js.length}}/*:any*/);
var hdr = o.header || [], C = 0;
for(var R = 0; R != js.length; ++R) {
Object.keys(js[R]).filter(function(x) { return js[R].hasOwnProperty(x); }).forEach(function(k) {
if((C=hdr.indexOf(k)) == -1) hdr[C=hdr.length] = k;
var v = js[R][k];
var t = 'z';
var s;
if(R == 0){
if(C<2)
{
s={
fill:{
fgColor:{ rgb: "FFFFAA00" }
}
,alignment:{ wrapText: true },
border:
{
bottom:{ style: "thick", color: "red" }
}
}
}else{
s={
fill:{
fgColor:{ rgb: "black" }
}
,alignment:{ wrapText: true },
border:
{
bottom:{ style: "thick", color: "red" }
}
}
}
}
if(typeof v == 'number') t = 'n';
else if(typeof v == 'boolean') t = 'b';
ws[XLSX.utils.encode_cell({c:C,r:R+1})] = {t:t, v:v,s:s};
});
}
range.e.c = hdr.length-1 ;
for(C = 0; C < hdr.length; ++C) ws[XLSX.utils.encode_col(C) + "1"] = {t:'s', v:hdr[C]};
ws['!ref'] = XLSX.utils.encode_range(range);
console.log("--------------")
console.log(ws)
return ws;
}
function Workbook() {
if(!(this instanceof Workbook)) return new Workbook();
this.SheetNames = [];
this.Sheets = {};
}
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;
}
function save(){
/* original data */
var data = [
{kkasfkdaskfkadskfadkfkdskfkdskfkdskfkdkfksdkflll: "kkasfkdaskfkadskfadkfkdskfkdskfkdskfkdkfksdkf"},
]
var ws_name = "SheetJS";
var orgheader = {header:["kkasfkdaskfkadskfadkfkdskfkdskfkdskfkdkfksdkflll"]};
var wb = new Workbook(), ws = json_to_sheet(data,orgheader);
/* add worksheet to workbook */
wb.SheetNames.push(ws_name);
console.log(ws);
console.log(JSON.stringify(ws));
wb.Sheets[ws_name] = ws;
var wbout = XLSX.write(wb, {bookType:'xlsx', bookSST:true, type: 'binary'});
console.log(JSON.stringify(wbout))
saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), "sample.xlsx")
}
</script>
</head><body>
<div>Click the button below to save a sample excel file with minimal styles.</div>
<button onclick="save()">save</button>
</body></html>