<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>示例</title>
</head>
<body>
<script src="./xlsx.full.min.js"></script>
<script>
/**
* @param datas 数据源 [{name: 'text',age: 18, code: '1'}]
* @param fieldNames 表头字段 ['name','age', 'code']
* @param headNames 表头名称 ['姓名', '年龄', '类型']
* @param toText code转name {code: function () {}}
* @param prompt 如果需要提示语会使用到它[{ text: '这是提示语1', merges: { s: { c: 0, r: 0 }, e: { c: 4, r: 0 } } }]
* @param fileName 文件名称
*/
const exportExcelFile = (datas, fieldNames, headNames, toText, prompt, fileName) => {
if (typeof fileName === 'undefined') return console.error('文件名称不存在')
if (!datas instanceof Array ||
!fieldNames instanceof Array ||
!headNames instanceof Array) {
return console.error('数据格式不正确')
}
if (!fieldNames.length || !headNames.length || fieldNames.length !== headNames.length) return console.error('表头字段和表头字段不匹配')
let data = [], r = 1, merges = [], cols =[]
if (prompt instanceof Array) {
prompt.map((val, i) => {
data[i] = [val.text]
if(val.merges instanceof Object){
merges.push(val.merges)
}
})
r = prompt.length
}
data = data.concat([headNames])
datas.map((item) => {
let row = []
headNames.map((val, i) => {
let name = fieldNames[i]
let value = item[name]
if (toText && toText[name] && typeof toText[name] === 'function') {
value = toText[name]({ row: item })
}
row.push(value)
cols.push({ wch: name.length + 8 })
})
data.push(row)
})
let sheetName = "Sheet1"; let ws = sheetFromArrayOfArrays(data, r)
ws["!merges"] = merges//<====合并单元格
ws['!cols'] = cols
console.log(ws)
let key = Object.keys(ws)
let tmpWB = {
SheetNames: ['mySheet'], // 保存的表标题
Sheets: {
'mySheet': Object.assign({},
ws, //内容
{
// '!ref': key[0] + ':' + key[key.length - 2] // 设置填充区域
})
}
}
tmpDown = new Blob([s2ab(XLSX.write(tmpWB,
{ bookType: 'xlsx', bookSST: false, type: 'binary' } // 这里的数据是用来定义导出的格式类型
))], {
type: ""
})
saveAs(tmpDown, fileName + '.xlsx')
}
const saveAs = (obj, fileName) => {
let tmpa = document.createElement("a")
tmpa.download = fileName || "下载"
tmpa.href = URL.createObjectURL(obj)
tmpa.click()
setTimeout(function () {
URL.revokeObjectURL(obj)
}, 100)
}
const s2ab = (s) => {
if (typeof ArrayBuffer !== 'undefined') {
let buf = new ArrayBuffer(s.length)
let view = new Uint8Array(buf)
for (let i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF
return buf
} else {
let buf = new Array(s.length)
for (let i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF
return buf
}
}
const sheetFromArrayOfArrays = (data, r) => {
let ws = {}
let range = { s: { c: 10000000, r: 10000000 }, e: { c: 0, r: 0 } }
for (let R = 0; R !== data.length; ++R) {
for (let C = 0; C !== data[R].length; ++C) {
if (range.s.r > R) range.s.r = R
if (range.s.c > C) range.s.c = C
if (range.e.r < R) range.e.r = R
if (range.e.c < C) range.e.c = C
let cell = { v: data[R][C] }
if (cell.v == null) continue
let cellRef = XLSX.utils.encode_cell({ c: C, r: R })
if (typeof cell.v === 'number') cell.t = 'n'
else if (typeof cell.v === 'boolean') cell.t = 'b'
else if (cell.v instanceof Date) {
cell.t = 'n'
cell.z = XLSX.SSF._table[14]
cell.v = datenum(cell.v)
} else cell.t = 's'
cell.s = {}
cell.s.font = { sz: 12, bold: false, color: { rgb: "000000" } }
if (R === r) { // 给表头添加颜色
cell.s = { font: { sz: 12, bold: false, color: { rgb: "000000" } }, fill: { bgColor: { indexed: 64 }, fgColor: { rgb: "e6e6e6" } } }
}
cell.s.alignment = {vertical: 'center', horizontal: 'top', wrapText: 'true'}
ws[cellRef] = cell
}
}
if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range)
return ws
}
const datenum = (v, date1904) => {
if (date1904) v += 1462
let epoch = Date.parse(v)
return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000)
}
</script>
<script>
function downloadExl() {
exportExcelFile(
[{ name: '姓名', age: '18', code: '1', text1: '文本1', text2: '文本2' },
{ name: '姓名', age: '18', code: '1', text1: '文本1', text2: '文本2' },
{ name: '姓名', age: '18', code: '1', text1: '文本1', text2: '文本2' },
{ name: '姓名', age: '18', code: '1', text1: '文本1', text2: '文本2' }],
['name', 'age', 'code', 'text1', 'text2'],
['姓名', '年龄', '类型', '内容', '简介'],
{code: onReturnText},
[
{ text: '这是提示语1', merges: { s: { c: 0, r: 0 }, e: { c: 4, r: 0 } } },
{ text: '这是提示语2'}
],
'export'
)
}
const onReturnText = (code) => {
return '类型1'
}
</script>
<button onclick="downloadExl()">导出</button>
</body>
</html>
xlsx-style实现表格下载
需积分: 47 16 浏览量
2020-12-17
09:41:39
上传
评论
收藏 215KB RAR 举报
shana0716
- 粉丝: 0
- 资源: 2
最新资源
- 激光所基于图像处理的QC代码,用MATLAB编写+源代码+文档说明
- 3018A-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 案例研究源代码(按章节).zip
- 基于qt+C++开发的截图工具+实现了截图后进行多种编辑类似QQ和微信的截图编辑功能(期末大作业&课设&项目开发)
- 2SJ345-VB一款SOT23封装P-Channel场效应MOS管
- 基于MATLAB的数字图像处理 学习资料整理+源代码+文档说明
- ios系统管理知识系统总结
- 根据B站‘’打浦桥程序员‘’发布的MATLAB GUI基础课程,自学完成了属于自己的一个matlab gui小工具+源代码+文档
- poco-1.12.5-msvc-x86
- 2SJ343-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0