<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>四岁导出</title>
</head>
<body>
<button onclick="downloadExl(jsono)">导出</button>
<a href="" download="我是导出来的测试文件.xlsx" id="downloadA"></a>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.core.min.js"></script>
<script>
var jsono = [{
"姓名": "四岁",
"年龄": "22",
"性别": "男"
}, {
"姓名": "老六",
"年龄": "6",
"性别": "男"
}, {
"姓名": "狗逼",
"年龄": "3",
"性别": "中性"
}];
var tmpDown;
function downloadExl(json, type) {
var tmpdata = json[0];
json.unshift({});
var keyMap = [];
for (var k in tmpdata) {
keyMap.push(k);
json[0][k] = k;
}
var tmpdata = [];
json.map(
(v, i) => keyMap.map((k, j) => Object.assign({}, {
v: v[k],
position: (j > 25 ? getCharCol(j) : String.fromCharCode(65 + j)) + (i + 1)
}))
)
.reduce((prev, next) => prev.concat(next)).forEach((v, i) => tmpdata[v.position] = {
v: v.v
});
var outputPos = Object.keys(tmpdata);
var tmpWB = {
SheetNames: ['mySheet'],
Sheets: {
'mySheet': Object.assign({},
tmpdata,
{
'!ref': outputPos[0] + ':' + outputPos[outputPos.length - 1]
})
}
};
tmpDown = new Blob([s2ab(XLSX.write(tmpWB,
{ bookType: (type == undefined ? 'xlsx' : type), bookSST: false, type: 'binary' }
))], {
type: ""
});
var href = URL.createObjectURL(tmpDown);
document.getElementById("downloadA").href = href;
document.getElementById("downloadA").click();
setTimeout(function () {
URL.revokeObjectURL(tmpDown);
}, 100);
}
function s2ab(s) {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
}
function getCharCol(n) {
let temCol = '',
s = '',
m = 0
while (n > 0) {
m = n % 26 + 1
s = String.fromCharCode(m + 64) + s
n = (n - m) / 26
}
return s
}
</script>
</body>
</html>
04-14
5222
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)