-
Notifications
You must be signed in to change notification settings - Fork 26
/
server.ts
70 lines (62 loc) · 1.56 KB
/
server.ts
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
import express from "express"
import { join } from "path"
import xlsx, { IJsonSheet, ISettings } from "json-as-xlsx"
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
const data: IJsonSheet[] = [
{
sheet: "Adults",
columns: [
{ label: "Name", value: "name" },
{ label: "Age", value: "age", format: '# "years"' },
],
content: [
{ name: "Monserrat", age: 21, more: { phone: "11111111" } },
{ name: "Luis", age: 22, more: { phone: "12345678" } },
],
},
{
sheet: "Pets",
columns: [
{ label: "Name", value: "name" },
{ label: "Age", value: "age" },
],
content: [
{ name: "Malteada", age: 4, more: { phone: "99999999" } },
{ name: "Picadillo", age: 1, more: { phone: "87654321" } },
],
},
]
const settings: ISettings = {
writeOptions: {
type: "buffer",
bookType: "xlsx",
},
}
app.get("/", (_, res) => {
res.sendFile(join(__dirname, "index.html"))
})
app.get("/get", (_, res) => {
const buffer = xlsx(data, settings)
res.writeHead(200, {
"Content-Type": "application/octet-stream",
"Content-disposition": "attachment; filename=MySheet.xlsx",
})
res.end(buffer)
})
app.get("/rtl", (_, res) => {
const buffer = xlsx(data, {
...settings,
RTL: true,
})
res.writeHead(200, {
"Content-Type": "application/octet-stream",
"Content-disposition": "attachment; filename=MySheet-RTL.xlsx",
})
res.end(buffer)
})
const port = 5500
app.listen(port, () => {
console.log("Your app is listening on port", port)
})