@@ -50,19 +50,61 @@ xlsx(data, settings) // Will download the excel file
5050
5151### TypeScript
5252
53+ Here is an example of a server setup using TS, thanks to @elyse0 for the contribution
54+
5355``` ts
54- const jsonSheets: IJsonSheet [] = [{
55- sheet: " Friends" ,
56- columns: [
57- { label: " Name" , value: " name" },
58- { label: " Username" , value: " username" }
59- ],
60- content: [
61- { name: " Andreas" , username: " andr34s" }
62- ]
63- }]
64-
65- xlsx (jsonSheets )
56+ import xlsx , { IJsonSheet , ISettings } from ' json-as-xlsx'
57+ import express from ' express'
58+
59+ const app = express ()
60+ app .use (express .json ())
61+ app .use (express .urlencoded ({ extended: true }))
62+
63+ const data: IJsonSheet [] = [
64+ {
65+ sheet: ' Adults' ,
66+ columns: [
67+ { label: ' User' , value: ' user' },
68+ { label: ' Age' , value: ' age' }
69+ ],
70+ content: [
71+ { user: ' Andrea' , age: 20 , more: { phone: ' 11111111' } },
72+ { user: ' Luis' , age: 21 , more: { phone: ' 12345678' } }
73+ ]
74+ }, {
75+ sheet: ' Children' ,
76+ columns: [
77+ { label: ' User' , value: ' user' },
78+ { label: ' Age' , value: ' age' }
79+ ],
80+ content: [
81+ { user: ' Manuel' , age: 16 , more: { phone: ' 99999999' } },
82+ { user: ' Ana' , age: 17 , more: { phone: ' 87654321' } }
83+ ]
84+ }
85+ ]
86+
87+ const settings: ISettings = {
88+ writeOptions: {
89+ type: ' buffer' ,
90+ bookType: ' xlsx'
91+ }
92+ }
93+
94+ app .get (' /' , (_ , res ) => {
95+ const buffer = xlsx (data , settings )
96+ res .writeHead (200 , {
97+ ' Content-Type' : ' application/octet-stream' ,
98+ ' Content-disposition' : ' attachment; filename=MySheet.xlsx'
99+ })
100+ res .end (buffer )
101+ })
102+
103+ const port = process .env .PORT ?? 3000
104+
105+ app .listen (port , () => {
106+ console .log (` Your app is listening on port ${port } ` )
107+ })
66108```
67109
68110## Examples
0 commit comments