首页 > 解决方案 > 将javascript对象写入xlsx,键为字符串,值为数组

问题描述

下面是要写入 excel.xlsx 文件的 Javascript 对象

{
 "emp1":['M','A','N','X','Y', 'Z'....],
 'emp2':['car','bike'],
 'emp3':['laptop','mouse']
}

以下是excel中的输出格式:-

员工 1 2 3 4 5 6
emp1 一个 ñ X 是的 Z
emp2 自行车
emp3 笔记本电脑

下面是我试图将输出转换为 excel 的代码

const writeIntoExcel = (shiftAssigned) => {
    console.log("shift Assigned", shiftAssigned);
    let content = JSON.parse(shiftAssigned);
    //  let content = shiftAssigned;
    let newWb = xlsx.utils.book_new();
    let newWs = xlsx.utils.json_to_sheet(content);
    let sheetName = (config.month + '_' + config.year);
    let fileName = (config.month + '_' + config.year + '.xlsx');
    // config.log("filename", fileName, sheetName);
    xlsx.utils.book_append_sheet(newWb, newWs, sheetName);
    xlsx.writeFile(newWb, fileName);
}
writeIntoExcel(shiftAssigned);

提前感谢您的帮助

标签: javascriptnode.js

解决方案


推荐阅读