html json 导出Excel

时间:2025-01-01 21:36:56
<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
<script src="http://oss.sheetjs.com/js-xlsx/xlsx.full.min.js"></script>
</head> <body>
<button id="button">导出Excel</button> <script>
var json = [
{ //测试数据
"保质期临期预警(天)": "adventLifecycle",
"商品标题": "title",
"建议零售价": "defaultPrice",
"高(cm)": "height",
"商品描述": "Description",
"保质期禁售(天)": "lockupLifecycle",
"商品名称": "skuName",
"商品简介": "brief",
"宽(cm)": "width",
"阿达": "asdz",
"货号": "goodsNo",
"商品条码": "skuNo",
"商品品牌": "brand",
"净容积(cm^3)": "netVolume",
"是否保质期管理": "isShelfLifeMgmt",
"是否串号管理": "isSNMgmt",
"商品颜色": "color",
"尺码": "size",
"是否批次管理": "isBatchMgmt",
"商品编号": "skuCode",
"商品简称": "shortName",
"毛重(g)": "grossWeight",
"长(cm)": "length",
"英文名称": "englishName",
"净重(g)": "netWeight",
"商品分类": "categoryId",
"这里超过了": 1111.0,
"保质期(天)": "expDate"
} ,{ //测试数据
"保质期临期预警(天)": "adventLifecycle22",
"商品标题": "title22",
"建议零售价": "defaultPrice2",
"高(cm)": "height2",
"商品描述": "Description2",
"保质期禁售(天)": "lockupLifecycle2",
"商品名称": "skuName2",
"商品简介": "brief2",
"宽(cm)": "width2",
"阿达": "asdz2",
"货号": "goodsNo2",
"商品条码": "skuNo2",
"商品品牌": "brand2",
"净容积(cm^3)": "netVolume2",
"是否保质期管理": "isShelfLifeMgmt2",
"是否串号管理": "isSNMgmt2",
"商品颜色": "color2",
"尺码": "size2",
"是否批次管理": "isBatchMgmt2",
"商品编号": "skuCode2",
"商品简称": "shortName2",
"毛重(g)": "grossWeight2",
"长(cm)": "length2",
"英文名称": "englishName2",
"净重(g)": "netWeight2",
"商品分类": "categoryId2",
"这里超过了": "1111.02",
"保质期(天)": "expDate2"
}]; document.getElementById("button").onclick = function(){
downloadExl(json, 'xlsx')
} var tmpDown; //导出的二进制对象
function downloadExl(json, type) {
var json = JSON.parse(JSON.stringify(json));
var tmpdata = json[0];
json.unshift({});
var keyMap = []; //获取keys
//keyMap =Object.keys(json[0]);
for (var k in tmpdata) {
keyMap.push(k);
json[0][k] = k;
}
var tmpdata = [];//用来保存转换好的json
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
});
console.log(json);
console.log(tmpdata); var outputPos = Object.keys(tmpdata); //设置区域,比如表格从A1到D10
var tmpWB = {
SheetNames: ['mySheet','my'], //保存的表标题
Sheets: {
'mySheet': Object.assign({},
tmpdata, //内容
{
'!ref': outputPos[0] + ':' + outputPos[outputPos.length - 1] //设置填充区域
}),
'my': Object.assign({},
tmpdata, //内容
{
'!ref': outputPos[0] + ':' + outputPos[outputPos.length - 1] //设置填充区域
}), }
};
console.log(tmpWB,'twb')
tmpDown = new Blob([s2ab(XLSX.write(tmpWB,
{bookType: (type == undefined ? 'xlsx':type),bookSST: false, type: 'binary'}//这里的数据是用来定义导出的格式类型
))], {
type: ""
}); //创建二进制对象写入转换好的字节流 var href = URL.createObjectURL(tmpDown); //创建对象超链接
       //模拟点击下载
var aDownLoad = document.createElement("a");
aDownLoad.href = href;
aDownLoad.download = 'href.xlsx';
aDownLoad.click(); setTimeout(function() {
URL.revokeObjectURL(tmpDown); //用URL.revokeObjectURL()来释放这个object URL
}, 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;
}
// 将指定的自然数转换为26进制表示。映射关系:[0-25] -> [A-Z]。
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>