-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.js
162 lines (140 loc) · 4.14 KB
/
app.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const xlsx = require('node-xlsx').default;
const fs = require('fs')
const xlsx2 = require('xlsx')
const form = {
name: '模拟数据表',
data: [
['姓名', '性别', '年级', '单位', '政治面貌', '籍贯'],
['zhangsan', 'man1', '21', 'home1', 'people', 'china'],
['zhangsan1', 'man2', '22', 'home2', 'people', 'china'],
['zhangsan2', 'man3', '23', 'home3', 'people', 'china'],
['zhangsan3', 'man4', '24', 'home4', 'people', 'china'],
['zhangsan4', 'man5', '25', 'home5', 'people', 'china'],
['zhangsan5', 'man6', '26', 'home6', 'people', 'china'],
['zhangsan6', 'man7', '27', 'home7', 'people', 'china'],
]
}
const form1 = {
name: '模拟数据表',
data: [
['姓名', '性别', '年级', '单位', '政治面貌', '籍贯'],
[
{
v: 'zhangsan',
s: {
font: {
size: 19,
bold: true,
color: {rgb: 'f33004'}
}
}
}
,
'man1', '21', 'home1', 'people', 'china'],
[{
v: 'zhangsan1',
s: {
font: {
size: 19,
bold: true,
color: {rgb: '573dff'}
},
fill: {
fgColor: {
rgb: 'ffff00'
}
},
}
}
, 'man2', '22', 'home2', 'people', 'china'],
['zhangsan2', 'man3', '23', 'home3', 'people', 'china'],
['zhangsan3', 'man4', '24', 'home4', 'people', 'china'],
['zhangsan4', 'man5', '25', 'home5', 'people', 'china'],
['zhangsan5', 'man6', '26', 'home6', 'people', 'china'],
['zhangsan6', 'man7', '27', 'home7', 'people', 'china'],
]
}
const mockData = []
const form2 = {
name: '认真的表格',
data: mockData
}
form.data.map((v, i) => {
if (i === 0) {
const firstLine = []
v.map((firstItem, i) => {
firstLine.push({
v: firstItem,
s: {
alignment: {
vertical: 'center',
horizontal: 'center'
},
font: {
size: 19,
bold: true,
color: {rgb: 'ffffff'}
},
fill: {
fgColor: {
rgb: 'a4a3a5'
}
}
}
})
})
mockData.push(firstLine)
} else {
const line = []
v.map((item, i) => {
line.push({
v: item,
s: {
alignment: {
vertical: 'center',
horizontal: 'center'
},
font: {
size: 19,
color: {rgb: 'ff280c'}
}
}
})
})
mockData.push(line)
}
})
const options = {
'!cols': [ //设置宽度
{wpx: 100},//1-姓名
{wpx: 140},//2-性别
{wpx: 180},//3-年级
{wpx: 220}, //4-单位
{wpx: 260}, //5-政治面貌
{wpx: 300}, //6-籍贯
],
//高度设置无效
'!rows': [//设置高度
{hpx: 40,}, //1
{hpx: 60},//2
{hpx: 80},//3
{hpx: 100},//4
{hpx: 120},//5
{hpx: 120},//6
{hpx: 120},//7
{hpx: 120},//8
{hpx: 120},//9
],
'!margins': {left: 0.7, right: 0.7, top: 0.75, bottom: 0.75, header: 0.3, footer: 0.3},
}
// const range = {s: {c: 0, r: 0}, e: {c: 0, r: 2}}; // A1:A4
// options['!merges'] = [range]
const xlsxData = xlsx.build([form2], options)
console.log("准备写入文件");
fs.writeFile('input.xlsx', xlsxData, function (err) {
if (err) {
return console.error(err);
}
console.log("数据写入成功!");
console.log("--------我是分割线-------------")
});