简体   繁体   中英

couldn't convert excel data to json object using convert-excel-to-json libraray

I am new to node.js. i want to build a web application where it takes input a file path (excel) and converts the data to json object (takes file input from input type file). So I have taken the file path and uploaded to the directory project directory and then fed that file to convert-excel-to-json.i was expecting a json object but could not get json object. The file uploading to the project directory works fine. Can anybody help? Thanks in advance

**index.js file

const express =require("express");
const bodyParser =require("body-parser");
const excelToJson=require("convert-excel-to-json");
const upload=require("express-fileupload");


const app=express();
app.use(bodyParser.urlencoded({extended:true}));
app.use(upload());


app.get("/",function(request,response){
    response.sendFile(__dirname+"/index.html");
})

app.post("/",function(request,response){

    if(request.files){
        let file=request.files.filesrc;
        let dest=__dirname+"/"+file.name;
        file.mv(dest);
        const result=excelToJson({
            sourceFile:dest,
        })
        response.send(result);
    }
})


app.listen(4001,function(){
    console.log("App is listening at port 4001");
})

**html file (index.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
        <form method="POST" action="/" enctype="multipart/form-data">
            <input type="file" name="filesrc" />
            <input type="submit" />
          </form>

</body>
</html>

expected output : json object

my output :{"Sheet1":[]}

The convert-excel-to-json NPM package doesn't return a JSON object as they clarify in their documentation .

it returns a normal object.

If you want to convert it to a JSON object you can use JSON.stringify() to convert JS objects to the JSON object

Ex:

response.send(JSON.stringify(result));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM