html XLSX.utils.json_to_sheet导出excel模板,由于此处是函数自调用,所以页面加载的时候就会提示下载
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>展示 用xlsx库 导出excel,含多个sheet</title>
</head>
<body>
<h1>展示 用xlsx库 导出excel,含多个sheet</h1>
<button id="export" style="padding:20px;background: #69f;color:#fff;" @click="aaa()"> 导出excel</button>
<script src="https://unpkg.com/xlsx/dist/xlsx.core.min.js"></script>
<script>
// 将workbook装化成blob对象
function workbook2blob(workbook) {
// 生成excel的配置项
var wopts = {
// 要生成的文件类型
bookType: "xlsx",
// // 是否生成Shared String Table,官方解释是,如果开启生成速度会下降,但在低版本IOS设备上有更好的兼容性
bookSST: false,
type: "binary"
};
var wbout = XLSX.write(workbook, wopts);
// 将字符串转ArrayBuffer
var blob = new Blob([s2ab(wbout)], {
type: "application/octet-stream"
});
return blob;
}
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;
}
// 将blob对象创建bloburl,然后用a标签实现弹出下载框
function openDownloadDialog(blob, fileName) {
if (typeof blob == "object" && blob instanceof Blob) {
blob = URL.createObjectURL(blob); // 创建blob地址
}
var aLink = document.createElement("a");
aLink.href = blob;
// HTML5新增的属性,指定保存文件名,可以不要后缀,注意,有时候 file:///模式下不会生效
aLink.download = fileName || "";
var event;
if (window.MouseEvent) event = new MouseEvent("click");
// 移动端
else {
event = document.createEvent("MouseEvents");
event.initMouseEvent( "click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null );
}
aLink.dispatchEvent(event);
}
// 用的例子
let sheet1data = [ { department: "行政部", count: 2 }, { department: "前端部", count: 2 } ];
let sheet2data = [ { name: "张三", do: "整理文件" }, { name: "李四", do: "打印" } ];
let sheet3data = [ { name: "张大人", do: "vue" }, { name: "李大人", do: "react" } ];
var sheet1 = XLSX.utils.json_to_sheet(sheet1data);
var sheet2 = XLSX.utils.json_to_sheet(sheet2data);
var sheet3 = XLSX.utils.json_to_sheet(sheet3data);
/* create a new blank workbook */
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, sheet1, "部门统计");
XLSX.utils.book_append_sheet(wb, sheet2, "行政部");
XLSX.utils.book_append_sheet(wb, sheet3, "前端部");
const workbookBlob = workbook2blob(wb);
openDownloadDialog(workbookBlob, `部门统计.xlsx`);
</script>
</body>
</html>
下面是vue,自己总结的
//找一个位置添加此按钮
<Button icon="ios-download-outline" type="primary" @click="exportData">模板导出</Button>
//此处是js
changeData (s) {
//如果存在ArrayBuffer对象(es6) 最好采用该对象
if (typeof ArrayBuffer !== 'undefined') {
//1、创建一个字节长度为s.length的内存区域
var buf = new ArrayBuffer(s.length);
//2、创建一个指向buf的Unit8视图,开始于字节0,直到缓冲区的末尾
var view = new Uint8Array(buf);
//3、返回指定位置的字符的Unicode编码
for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
} else {
var buf2 = new Array(s.length);
for (var k = 0; k != s.length; ++k) buf2[k] = s.charCodeAt(k) & 0xFF;
return buf2;
}
},
saveAs (obj, fileName) {//当然可以自定义简单的下载文件实现方式
var tmpa = document.createElement("a");
tmpa.download = fileName ? fileName + '.xlsx' : new Date().getTime() + '.xlsx';
tmpa.href = URL.createObjectURL(obj); //绑定a标签
tmpa.click(); //模拟点击实现下载
setTimeout(function () { //延时释放
URL.revokeObjectURL(obj); //用URL.revokeObjectURL()来释放这个object URL
}, 100);
},
//按钮调用的方法
exportData () {
var wopts = {
bookType: 'xlsx',
bookSST: false,
type: 'binary'
};
var workBook = {
SheetNames: ['Sheet1'],
Sheets: {},
Props: {}
};
//设置excel表格的表头
let sheet1data = [{ "销售物料代码": "", "采购物料代码": "", "下单数量": "" }]
//1、XLSX.utils.json_to_sheet(data) 接收一个对象数组并返回一个基于对象关键字自动生成的“标题”的工作表,默认的列顺序由使用Object.keys的字段的第一次出现确定
//2、将数据放入对象workBook的Sheets中等待输出
workBook.Sheets['Sheet1'] = XLSX.utils.json_to_sheet(sheet1data)
//3、XLSX.write() 开始编写Excel表格
//4、changeData() 将数据处理成需要输出的格式
this.saveAs(new Blob([this.changeData(XLSX.write(workBook, wopts))], { type: 'application/octet-stream' }))
},