https://github.com/eligrey/FileSaver.js
<html>
<head>
<meta charset="UTF-8">
<title>导出生成json文件和文本title>
<script src="js/FileSaver.js">script>
head>
<body>
<button onclick="downloadJson(students)">导出生成json文件button>
<button onclick="downloadText(students)">导出生成文本button>
<button onclick="downloadCsv()">导出生成表格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文件
* @param {Object} data
*/
function downloadJson(data) {
var blob = new Blob([JSON.stringify(data)], {
type: ""
});
saveAs(blob, "hello.json");
}
/**
* 导出生成文本
* @param {Object} data
*/
function downloadText(data) {
var blob = new Blob([JSON.stringify(data)], {
type: "text/plain;charset=utf-8"
});
saveAs(blob, "hello.txt");
}
/**
* 导出Excel
*/
function downloadCsv() {
var res = ['标题一,标题二,标题三,标题四', '数据1,数据2,数据3,数据4', '数据1,数据2,数据3,数据4', '数据1,数据2,数据3,数据4']
var file = new File([res.join('\r\n')], "hello.csv", {
type: "text/plain;charset=utf-8"
});
saveAs(file);
}
script>
html>