Javascript中的Excel到JSON模式

我有一个生成表单excelsheet的任务,我将根据excelsheet中提供的数据类型设计表单.例:

《Javascript中的Excel到JSON模式》

我正在尝试从上面的Excel数据中创建一个JSON模式,以便我可以将其插入到mongodb中以动态生成表单.

以下是我正在尝试实现的代码:

            var workbook = XLSX.readFile(req.file.path);
            //console.log(workbook);
            var result = {};
            workbook.SheetNames.forEach(function (sheetName) {
                var roa = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
                if (roa.length > 0) {
                    result = roa;
                }
            });
            //return result;
            //console.log(result);

            var jsonData = {};
            var dropdown = {};
            var attrTypes = result[0];
            //console.log(attrTypes);

            for (var i = 1; i < result.length; i++) {
                var obj = result[i];
                //console.log(obj);
                for (var key in obj) {
                    var attrName = key;
                    var attrValue = obj[key];
                    if (attrTypes[attrName]) {
                        var type = attrTypes[attrName].toLowerCase().replace(/ /g, ''); // Means type is given                        
                        //console.log(type);

                        if (type === "selectbox") {
                            console.log(attrValue);
                            //var dropdown = attrValue;
                            //console.log(dropdown);
                        }

                    } else {
                        //console.log(type); // Means type is not given
                        jsonData = attrName + ":" + attrValue;
                        //console.log(jsonData);
                    }
                }
            }

预期的JSON输出:

[
{
    Number : 1,
    FirstName : "Abc",
    LastName : "Xyza",
    Dept: ['Finance','Health','Insurance'],
    Country : ['US','Australia','Canada'],
    Year : ['2014','2015','2016'],,
    DateofBirth" : new Date(1937,05,02),
    Gender : ['M','F']    
},
{
    Number : 2,
    FirstName : "Abcd",
    LastName : "Xyzb",
    Dept: ['Finance','Health','Insurance'],
    Country : ['US','Australia','Canada'],
    Year : ['2014','2015','2016'],,
    DateofBirth" : new Date(1948,10,27),
    Gender : ['M','F']    
}
        .
        .
        and so on
]

以上是我试图在MEANSTACK中实现的代码.

任何帮助,将不胜感激.

最佳答案 您可以使用
JS-XLSX库在客户端和其他excel格式上阅读XLSX.

您不需要将下拉列表中存储的任何其他值存储在输入中.它们应该分开存放.所以数组对象应该如下所示

[   
    {  
      "Number":1,
      "FirstName":"Abc",
      "LastName":"Xyza",
      "Dept":"Finance",
      "Country":"US",
      "Year":2014,
      "DateOfBirth":19370502,
      "Gender":"M"
    },
    {  
      "Number":2,
      "FirstName":"Abcd",
      "LastName":"Xyzb",
      "Dept":"Health",
      "Country":"Australia",
      "Year":2014,
      "DateOfBirth":19481027,
      "Gender":"F"
    }
]

下拉和无线电值应分开存储,如下所示:

{  
   "Dept":{  
      "type":"dropdown",
      "values":[  
         "Finance",
         "Health",
         "Insurance"
      ]
   },
   "Country":{  
      "type":"dropdown",
      "values":[  
         "US",
         "Australia",
         "Canada"
      ]
   },
   "Year":{  
      "type":"dropdown",
      "values":[  
         2014,
         2015,
         2016
      ]
   },
   "Gender":{  
      "type":"radio button",
      "values":[  
         "M",
         "F"
      ]
   }
}

这两者都可以组合成一个模式对象

//included single objects from both for brevity
jsonSchema = {
    array: [
        {  
            "Number":2,
            "FirstName":"Abcd",
            "LastName":"Xyzb",
            "Dept":"Health",
            "Country":"Australia",
            "Year":2014,
            "DateOfBirth":19481027,
            "Gender":"F"
        }
    ],
    inputs: {
        "Gender":{  
            "type":"radio button",
            "values":[  
                "M",
                "F"
            ]
        }
    }
};

注意:序列化为JSON时,日期类型值不能存储为日期对象.这些应存储为字符串或数字,并应在客户端转换为Date对象

我在这个GIT项目中实现了JSON生成和表单生成

https://github.com/ConsciousObserver/stackoverflow/tree/master/excelTest

以下是输出截图.

《Javascript中的Excel到JSON模式》

这是输出JSON.

{
  "array": [
    {
      "Number": 1,
      "FirstName": "Abc",
      "LastName": "Xyza",
      "Dept": "Finance",
      "Country": "US",
      "Year": 2014,
      "DateOfBirth": 19370502,
      "Gender": "M"
    },
    {
      "Number": 2,
      "FirstName": "Abcd",
      "LastName": "Xyzb",
      "Dept": "Health",
      "Country": "Australia",
      "Year": 2014,
      "DateOfBirth": 19481027,
      "Gender": "F"
    },
    {
      "Number": 3,
      "FirstName": "Abce",
      "LastName": "Xyzc",
      "Dept": "Health",
      "Country": "US",
      "Year": 2015,
      "DateOfBirth": 19441029,
      "Gender": "F"
    },
    {
      "Number": 4,
      "FirstName": "Abcf",
      "LastName": "Xyzd",
      "Dept": "Insurance",
      "Country": "Canada",
      "Year": 2016,
      "DateOfBirth": 19481030,
      "Gender": "M"
    },
    {
      "Number": 5,
      "FirstName": "Abcg",
      "LastName": "Xyze",
      "Dept": "Finance",
      "Country": "Canada",
      "Year": 2016,
      "DateOfBirth": 19480604,
      "Gender": "M"
    }
  ],
  "inputs": {
    "Dept": {
      "type": "dropdown",
      "values": [
        "Finance",
        "Health",
        "Insurance"
      ]
    },
    "Country": {
      "type": "dropdown",
      "values": [
        "US",
        "Australia",
        "Canada"
      ]
    },
    "Year": {
      "type": "dropdown",
      "values": [
        2014,
        2015,
        2016
      ]
    },
    "Gender": {
      "type": "radio button",
      "values": [
        "M",
        "F"
      ]
    }
  }
}
点赞