json to excel js

墨色 4月前 ⋅ 295 阅读

代码如下,直接新建服务为html

<!doctype html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    
    <title>Document</title>
</head>

<body>
    <div>
        <input type="file" id="files" />
        <button onclick="jsonToExcel()">点击导出为Excel</button>
    </div>
    
        var json;
        var inputElement = document.getElementById("files");
        inputElement.addEventListener("change", handleFiles, false);
        function handleFiles() {
            var selectedFile = document.getElementById("files").files[0];
            var reader = new FileReader();
            reader.readAsText(selectedFile, "UTF8");
            reader. function () {
                json = JSON.parse(this.result);
            };
        }
        function jsonToExcel() {
            var filename = "Result.xlsx";
            var ws_name = "SheetJS";
            var wb = XLSX.utils.book_new();
            var ws = XLSX.utils.json_to_sheet(json, { raw: true });
            XLSX.utils.book_append_sheet(wb, ws, ws_name);
            XLSX.writeFile(wb, filename);
        }
    
</body>

</html>

全部评论: 0

    我有话说: