加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
demo.html 1.84 KB
一键复制 编辑 原始数据 按行查看 历史
worklz 提交于 2021-05-10 21:44 . first
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<script src="./libs/xlsx.core.min.js"></script>
<script src="./libs/xlsx.style.min.js"></script>
</head>
<body>
<h1>整合<a href="https://github.com/SheetJS/js-xlsx" target="_blank">sheetjs</a><a href="https://github.com/protobi/js-xlsx" target="_blank">xlsx-style</a>导出自定义样式Excel的示例代码</h1>
<script type="text/javascript">
((win,doc) => {
function str_to_array_buffer(str){
let buf = new ArrayBuffer(str.length);
let view = new Uint8Array(buf);
for (let i=0;i!==str.length;++i) view[i] = str.charCodeAt(i) & 0xFF;
return buf;
}
// 设置数据
let wb = {
SheetNames: ["sheet1"],
Sheets: {},
};
let data = [
["标题1","标题2","标题3"],
["123","456","789"]
];
let ws = XLSX.utils.aoa_to_sheet(data);
// 设置样式
ws["A1"].s = {
fill: { fgColor: { rgb: 'FFFFFF00' } }
};
wb.Sheets["sheet1"] = ws;
// 导出保存
let wopts = {bookType: 'xlsx',bookSST: false,type: 'binary'};
let wbout = XlsxStyle.write(wb, wopts);
let blob = new Blob([str_to_array_buffer(wbout)], {type:"application/octet-stream"});
let url = window.URL.createObjectURL(blob);
let link = doc.createElement('a');
let body = doc.querySelector('body');
link.href = url;
link.download = "demo.xlsx";
// fix Firefox
link.style.display = 'none';
body.appendChild(link);
link.click();
body.removeChild(link);
win.URL.revokeObjectURL(link.href);
})(window,document);
</script>
</body>
</html>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化