Dependency Package: multiparty,xlsx, The code is as follows:
varMultiparty = require (' multiparty '));varxlsx = require ("xlsx");varform =Newmultiparty. Form ();functionTo_json (workbook,id) {//get all table names in Excel, return [' Sheet1 ', ' Sheet2 '] varSheetnames =workbook. Sheetnames; Sheetnames.foreach (function(sheetname) {//gets the data for each sheet. varWorksheet =workbook. Sheets[sheetname]; varTemparray =XLSX.utils.sheet_to_json (worksheet); Temparray.foreach (function(ele) {//get each recordEle.state = Ele.state | | 0; Ele.create_time= Ele.modify_time =utils.formatdate (); Ele.modify_user=username; }); Result[sheetname]=Temparray; }); //all the data has been read into memory. returnresult;} Form.parse (req,function(Err, fields, files) {if(err) {Throw NewError ("Form is Error"); } /*{"Filetoupload": [{"FieldName": "Filetoupload", "OriginalFilename": "Test.xlsx", "Path": "/var/folders/tk/jwcqj5_x74lgdzfcdhv248x00000gn/t/tilxemnkwt5v2yondxpxnees.xlsx", "headers": { "Content-disposition": "Form-data; Name=\ "filetoupload\"; Filename=\ "Test.xlsx\" "," Content-type ":" Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet "}," Size ": 9389}]}*/ varfilename = files.filetoupload[0].originalfilename; varSuffixarray = Filename.split ('. ')); varsuffix = suffixarray[suffixarray.length-1].tolowercase (); if(Suffix! = "xlsx" && suffix! = "xls"){ Throw NewError ("Upload only files in xlsx,xls format"); }Else{ //the path of the server temporary storage file varfilepath = files.filetoupload[0].path; Console.log (The path of the xlsx file read is: "+filepath); varWorkbook =Xlsx.readfile (filepath); varresult =To_json (workbook,req.session.login_username); //Delete the path to the server temporary storage fileFs.unlink (filepath,function(Err) {Console.error (filepath+ "File deletion failed!" "+err); }); }});
Nodejs reading xlsx files