【问题标题】:I want to convert excel data into multiple Json in Javascript/Typescript我想在 Javascript/Typescript 中将 excel 数据转换为多个 Json
【发布时间】:2021-12-31 11:34:24
【问题描述】:

我的 Excel 有许多数据表,我想将其转换为 javascript/typescript 中的 Json。以下是我在一张表中的 Excel 数据示例,由一个或多个空行分隔。

Name Age
John 40
Olivia 30
EmpId Salary LanguagesKnown
43567 15000 English,French
98543 20000 Arab,Chinese

我想把这些转换成 Json。

预期输出:

**Json for first table:**

{
"Name" : "John",
"Age" : 40
}

{
"Name" : "Olivia",
"Age" : 30
}

**Json for Second Table:**

{
"EmpId" : 43567,
"Salary" : 15000,
"LanguagesKnown" : ["English","French"]
}

{
"EmpId" : 98543,
"Salary" : 20000,
"LanguagesKnown" : ["Arab","Chinese"]
}

【问题讨论】:

  • 是的,我知道如何读取 excel 数据进行映射(使用 apache poi),然后在 java 中转换为 POJO 类。但我不知道如何在javascript中做。我知道这不是 Fiverr。我认为人们会在这里给出解决方案/指针。
  • 您没有提供任何代码,给人的印象是您希望我们为您的规范创建完整的解决方案。阅读how to ask。在堆栈溢出时发布应该始终是very last thing you try。例如,您是否尝试过使用谷歌搜索 node.js 的 excel 库?如果是这样,您为什么不在问题中包含它?如果没有,为什么不呢?
  • 是的,你是对的。我也应该包含代码。主要问题是所有 node.js 库,如 sheetJs 或其他 utlis 将整个工作表转换为 json。但我的问题是将excel的一部分转换为json。我是 javascript 的新手,它有一个类似于 json 的对象。所以我认为可以有一种简单的方法,而不是通过在 java 中使用 POJO 将 excel 转换为 map 并构造 json。

标签: javascript node.js json excel typescript


【解决方案1】:

js-xlsx 库可以将 Excel/CSV 文件转换为 JSON 对象。

<!doctype html>
<html>

<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.15.6/xlsx.full.min.js"></script>
</head>

<body>

    <script>
        var url = "https://file-examples-com.github.io/uploads/2017/02/file_example_XLS_10.xls"; //Excel File
        var oReq = new XMLHttpRequest();
        oReq.open("GET", url, true);
        oReq.responseType = "arraybuffer";

        oReq.onload = function(e) {
            var arraybuffer = oReq.response;

            /* convert to binary */
            var data = new Uint8Array(arraybuffer);
            var arr = new Array();
            for (var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
            var bstr = arr.join("");

            var workbook = XLSX.read(bstr, {
                type: "binary"
            });

            var first_sheet_name = workbook.SheetNames[0]; //Excel sheet No
            var worksheet = workbook.Sheets[first_sheet_name];
            const convertToJson = XLSX.utils.sheet_to_json(worksheet, {
                raw: true
            })
            console.log(convertToJson);
        }

        oReq.send();
    </script>
</body>
</html>

输出:

[
    {
        "0": 1,
        "First Name": "Dulce",
        "Last Name": "Abril",
        "Gender": "Female",
        "Country": "United States",
        "Age": 32,
        "Date": "15/10/2017",
        "Id": 1562
    },
    {
        "0": 2,
        "First Name": "Mara",
        "Last Name": "Hashimoto",
        "Gender": "Female",
        "Country": "Great Britain",
        "Age": 25,
        "Date": "16/08/2016",
        "Id": 1582
    },
    {
        "0": 3,
        "First Name": "Philip",
        "Last Name": "Gent",
        "Gender": "Male",
        "Country": "France",
        "Age": 36,
        "Date": "21/05/2015",
        "Id": 2587
    },
    {
        "0": 4,
        "First Name": "Kathleen",
        "Last Name": "Hanner",
        "Gender": "Female",
        "Country": "United States",
        "Age": 25,
        "Date": "15/10/2017",
        "Id": 3549
    },
    {
        "0": 5,
        "First Name": "Nereida",
        "Last Name": "Magwood",
        "Gender": "Female",
        "Country": "United States",
        "Age": 58,
        "Date": "16/08/2016",
        "Id": 2468
    },
    {
        "0": 6,
        "First Name": "Gaston",
        "Last Name": "Brumm",
        "Gender": "Male",
        "Country": "United States",
        "Age": 24,
        "Date": "21/05/2015",
        "Id": 2554
    },
    {
        "0": 7,
        "First Name": "Etta",
        "Last Name": "Hurn",
        "Gender": "Female",
        "Country": "Great Britain",
        "Age": 56,
        "Date": "15/10/2017",
        "Id": 3598
    },
    {
        "0": 8,
        "First Name": "Earlean",
        "Last Name": "Melgar",
        "Gender": "Female",
        "Country": "United States",
        "Age": 27,
        "Date": "16/08/2016",
        "Id": 2456
    },
    {
        "0": 9,
        "First Name": "Vincenza",
        "Last Name": "Weiland",
        "Gender": "Female",
        "Country": "United States",
        "Age": 40,
        "Date": "21/05/2015",
        "Id": 6548
    }
]

【讨论】:

    【解决方案2】:

    我使用async-csv包

    import * as fs from "fs";
    import csv from 'async-csv';
    
    const fsInstance = fs.promises;
    
    const csvString = await fsInstance.readFile(mailListFound.mailListPath, 'utf-8');
    const rows = await csv.parse(csvString);
    rows.shift();
    const data = JSON.parse(JSON.stringify(rows));
    

    数据变量中的数据现在是您可以使用的 JSON。 注意:在使用 JSON.parse 解析之前,我使用 rows.shift() 从数据中删除标题行。

    我的示例文件如下所示

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 2018-07-05
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      • 2021-04-06
      • 1970-01-01
      相关资源
      最近更新 更多