1+ var stream = require ( 'stream' ) ;
2+ var await = require ( 'await' )
3+
4+ const db = require ( '../config/db.config.js' ) ;
5+ const Customer = db . Customer ;
6+
7+ const excel = require ( 'exceljs' ) ;
8+
9+ const readXlsxFile = require ( 'read-excel-file/node' ) ;
10+
11+ exports . uploadFile = ( req , res ) => {
12+
13+ try {
14+ let filePath = __basedir + "/uploads/" + req . file . filename ;
15+
16+ readXlsxFile ( filePath ) . then ( rows => {
17+ // `rows` is an array of rows
18+ // each row being an array of cells.
19+ console . log ( rows ) ;
20+
21+ // Remove Header ROW
22+ rows . shift ( ) ;
23+
24+ const customers = [ ] ;
25+
26+ let length = rows . length ;
27+
28+ for ( let i = 0 ; i < length ; i ++ ) {
29+
30+ let customer = {
31+ id : rows [ i ] [ 0 ] ,
32+ name : rows [ i ] [ 1 ] ,
33+ address : rows [ i ] [ 2 ] ,
34+ age : rows [ i ] [ 3 ]
35+ }
36+
37+ customers . push ( customer ) ;
38+ }
39+
40+ Customer . bulkCreate ( customers ) . then ( ( ) => {
41+ const result = {
42+ status : "ok" ,
43+ filename : req . file . originalname ,
44+ message : "Upload Successfully!" ,
45+ }
46+
47+ res . json ( result ) ;
48+ } ) ;
49+ } ) ;
50+ } catch ( error ) {
51+ const result = {
52+ status : "fail" ,
53+ filename : req . file . originalname ,
54+ message : "Upload Error! message = " + error . message
55+ }
56+ res . json ( result ) ;
57+ }
58+ }
59+
60+ /**
61+ * Upload multiple Excel Files
62+ *
63+ * @param {* } req
64+ * @param {* } res
65+ */
66+ exports . uploadMultipleFiles = async ( req , res ) => {
67+ const messages = [ ] ;
68+
69+ for ( const file of req . files ) {
70+ try {
71+ let filePath = __basedir + "/uploads/" + file . filename ;
72+ let rows = await readXlsxFile ( filePath ) ;
73+
74+ // `rows` is an array of rows
75+ // each row being an array of cells.
76+ console . log ( rows ) ;
77+
78+ // Remove Header ROW
79+ rows . shift ( ) ;
80+
81+ const customers = [ ] ;
82+
83+ let length = rows . length ;
84+
85+ for ( let i = 0 ; i < length ; i ++ ) {
86+
87+ let customer = {
88+ id : rows [ i ] [ 0 ] ,
89+ name : rows [ i ] [ 1 ] ,
90+ address : rows [ i ] [ 2 ] ,
91+ age : rows [ i ] [ 3 ]
92+ }
93+
94+ customers . push ( customer ) ;
95+ }
96+
97+ uploadResult = await Customer . bulkCreate ( customers ) ;
98+
99+ // It will now wait for above Promise to be fulfilled and show the proper details
100+ console . log ( uploadResult ) ;
101+
102+ if ( ! uploadResult ) {
103+ const result = {
104+ status : "fail" ,
105+ filename : file . originalname ,
106+ message : "Can NOT upload Successfully" ,
107+ }
108+
109+ messages . push ( result ) ;
110+ } else {
111+ const result = {
112+ status : "ok" ,
113+ filename : file . originalname ,
114+ message : "Upload Successfully!" ,
115+ }
116+ messages . push ( result ) ;
117+ }
118+ } catch ( error ) {
119+ const result = {
120+ status : "fail" ,
121+ filename : file . originalname ,
122+ message : "Error -> " + error . message
123+ }
124+
125+ messages . push ( result ) ;
126+ }
127+ }
128+
129+ return res . json ( messages ) ;
130+ }
131+
132+ exports . downloadFile = ( req , res ) => {
133+ Customer . findAll ( ) . then ( objects => {
134+ var customers = [ ] ;
135+ let length = objects . length ;
136+
137+ for ( let i = 0 ; i < length ; i ++ ) {
138+ let datavalues = objects [ i ] . dataValues ;
139+ let customer = {
140+ id : datavalues . id ,
141+ name : datavalues . name ,
142+ address : datavalues . address ,
143+ age : datavalues . age
144+ } ;
145+ customers . push ( customer ) ;
146+ }
147+
148+ console . log ( customers ) ;
149+
150+ const jsonCustomers = JSON . parse ( JSON . stringify ( customers ) ) ;
151+
152+ let workbook = new excel . Workbook ( ) ; //creating workbook
153+ let worksheet = workbook . addWorksheet ( 'Customers' ) ; //creating worksheet
154+
155+ worksheet . columns = [
156+ { header : 'Id' , key : 'id' , width : 10 } ,
157+ { header : 'Name' , key : 'name' , width : 30 } ,
158+ { header : 'Address' , key : 'address' , width : 30 } ,
159+ { header : 'Age' , key : 'age' , width : 10 , outlineLevel : 1 }
160+ ] ;
161+
162+ // Add Array Rows
163+ worksheet . addRows ( jsonCustomers ) ;
164+
165+ res . setHeader ( 'Content-Type' , 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ) ;
166+ res . setHeader ( 'Content-Disposition' , 'attachment; filename=' + 'customer.xlsx' ) ;
167+
168+ return workbook . xlsx . write ( res )
169+ . then ( function ( ) {
170+ res . status ( 200 ) . end ( ) ;
171+ } ) ;
172+ } ) ;
173+ }
0 commit comments