FileSaver.js导出json文件和文本

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>导出生成json文件和文本</title>
    <script src="FileSaver.js"></script>
</head>

<body>
<button onclick="downloadJson(students)">导出生成json文件</button>
<button onclick="downloadText(students)">导出生成文本</button>
</body>
<script>
    var students = [{
        "name": "小明1",
        "age": "6",
        "sex": "男",
        "height": "60"
    }, {
        "name": "小明2",
        "age": "7",
        "sex": "男",
        "height": "70"
    }, {
        "name": "小明3",
        "age": "8",
        "sex": "男",
        "height": "80"
    }];
    // 导出生成json文件
    function downloadJson(data) {
        var blob = new Blob([JSON.stringify(data)], { type: "" });
        saveAs(blob, "hello.json");
    }
    // 导出生成文本
    function downloadText(data) {
        var blob = new Blob([JSON.stringify(data)], { type: "text/plain;charset=utf-8" });
        saveAs(blob, "hello.txt");
    }

</script>
</html>

转:https://www.cnblogs.com/absolute-child/p/8111541.html

当导出的是json文件或是txt文件时,导出的内容要是字符串,特别当时导出的数据是json数据时,要记得转一下。

转为Excel数据,见http://www.cnblogs.com/absolute-child/p/8083129.html

猜你喜欢

转载自blog.csdn.net/wt346326775/article/details/83617663