将excel(.xlsx)文件转换为JSON

时间:2017-11-14 10:19:05

标签: node.js excel mongodb mongoose

我有excel表名为sampledata.xlsx,我将其转换为json和console.log以打印此数据。



datatype: "html",




this doc的帮助下。 我想在这里做的是,在我的sampledata.xlsx文件中,我有更多数据,如平面,地址,价格等,我已经不知道 excel表中有哪些字段但我希望所有这些都是console.log。我能做到这一点有没有办法做到这一点。

2 个答案:

答案 0 :(得分:1)

从'xlsx2json'导入xlsx2json;

        OR

const xlsx2json = require('xlsx2json');

const excel2json = [
(req, res, next) => {
xlsx2json(req.body.file.path)
  .then(result => result[0])
  .reduce((object, item, index) => {
     if (index === 0) {
      object.mapper = item;  // eslint-disable-line no-param-reassign
      return object;
     }
  const data = {};
  Object.keys(item).forEach((key) => {
    data[object.mapper[key]] = item[key];
  });
  object.data.push(data);
    return object;
 }, { mapper: {}, data: [] })
   .then(excel => console.log(excel)) // this gives json as output
   .catch(err => next(err));
 },
];

答案 1 :(得分:0)

var exceltojson = require("xls-to-json-lc");
  exceltojson({
    input: "pass the input excel file here (.xls format)"
    output: "if you want output to be stored in a file"
    sheet: "sheetname",  // specific sheetname inside excel file (if you have multiple sheets)
    lowerCaseHeaders:true //to convert all excel headers to lowr case in json
  }, function(err, result) {
    if(err) {
      console.error(err);
    } else {
      console.log(result);
      //result will contain the overted json data
    }
  });

{{1}}