繁体   English   中英

无法使用 convert-excel-to-json 库将 excel 数据转换为 json 对象

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

我是 node.js 的新手。 我想构建一个 web 应用程序,它需要输入文件路径(excel)并将数据转换为 json 对象(从输入类型文件获取文件输入)。 所以我取了文件路径并上传到目录项目目录,然后将该文件提供给convert-excel-to-json。我期待一个json对象但无法获取json对象。 上传到项目目录的文件工作正常。 有人可以帮忙吗? 提前致谢

**index.js 文件

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 文件 (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>

预期输出:json 对象

我的输出:{“Sheet1”:[]}

convert-excel-to-json NPM 包不返回 JSON 对象,正如他们在文档中阐明的那样。

它返回一个普通对象。

如果要将其转换为 JSON 对象,可以使用JSON.stringify()将 JS 对象转换为 JSON 对象

前任:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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