1.自定义导出组件
点击查看代码
<template>
<div></div>
</template>
<script>
import XLSX from "xlsx-js-style";
export default {
name: "xlsxdown",
methods: {
downloadExl(json, dataTitle, fileName = "导出文件",paramStyle) {
if(json.length ==0) return;
const type = {
bookType: "xlsx",
bookSST: true,
type: "binary",
cellStyles: true,
};
let col = [];
let tmpdata = json[0];
var keyMap = [];
json.unshift({});
for (var k in tmpdata) {
keyMap.push(k);
col.push({ wpx: paramStyle.w || 150});
json[0][k] = k;
}
let titleIndex = dataTitle ? 2 : 1;
let borderAll = {
top: {
style: paramStyle.bdType||"thin",
color: {
rgb: paramStyle.bdColor||"00000000",
},
},
bottom: {
style: paramStyle.bdType||"thin",
color: {
rgb: paramStyle.bdColor||"00000000",
},
},
left: {
style: paramStyle.bdType||"thin",
color: {
rgb: paramStyle.bdColor||"00000000",
},
},
right: {
style: paramStyle.bdType||"thin",
color: {
rgb: paramStyle.bdColor||"00000000",
},
},
};
let styleCell = {
border: borderAll,
alignment: { vertical: "center", horizontal: "center" },
};
json
.map((v, i) => {
let data = keyMap.map((k, j) => {
return Object.assign(
{},
{
v: v[k],
position:
(j > 25 ? this.getCharCol(j) : String.fromCharCode(65 + j)) +
(i + titleIndex),
}
);
});
return data;
})
.reduce((prev, next) => prev.concat(next))
.forEach((v, i) => {
tmpdata[v.position] = { v: v.v, s: styleCell };
});
var outputPos = Object.keys(tmpdata);
outputPos = ["A1"].concat(outputPos);
if (dataTitle) {
tmpdata["A1"] = { v: dataTitle, s: {
border: borderAll,
alignment: { vertical: "center", horizontal: "center" },
font: { sz: 14, bold: true},
fill: {fgColor: { rgb: "E8E8E8" } },
} };
tmpdata["!merges"] = [
{
s: { c: 0, r: 0 },
e: { c: col.length-1, r: 0 },
},
];
}
tmpdata["!cols"] = col;
var tmpWB = {
SheetNames: ["mySheet"],
Sheets: {
mySheet: Object.assign(
{},
tmpdata,
{
"!ref": outputPos[0] + ":" + outputPos[outputPos.length - 1],
}
),
},
};
var tmpDown = new Blob(
[
this.s2ab(
XLSX.write(
tmpWB,
{
bookType: type == undefined ? "xlsx" : type.bookType,
bookSST: false,
type: "binary",
}
)
),
],
{
type: "",
}
);
this.saveAs(
tmpDown,
fileName + "." + (type.bookType == "biff2" ? "xls" : type.bookType)
);
},
saveAs(obj, fileName) {
var tmpa = document.createElement("a");
tmpa.download = fileName || "未命名";
if ("msSaveOrOpenBlob" in navigator) {
window.navigator.msSaveOrOpenBlob(obj, "导出文件" + ".xlsx");
} else {
tmpa.href = URL.createObjectURL(obj);
}
tmpa.click();
setTimeout(function () {
URL.revokeObjectURL(obj);
}, 100);
},
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;
},
s2ab(s) {
if (typeof ArrayBuffer !== "undefined") {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var j = 0; j != s.length; ++j) {
view[j] = s.charCodeAt(j) & 0xff;
}
return buf;
} else {
buf = new Array(s.length);
for (var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xff;
return buf;
}
},
},
};
</script>
2.安装插件依赖(xlsx、xlsx-js-style)
(1)例:npm i xlsx@0.18.5
(2)例:npm i xlsx-js-style@1.2.0
3.utils封装函数
点击查看代码
export function formatDate(date, str) {
date = new Date(date)
str = str ? str : 'yyyy/MM/dd hh:mm:ss'
if (/(y+)/.test(str)) {
str = str.replace(RegExp.$1, String(date.getFullYear()).substr(4 - RegExp.$1.length))
}
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
}
for (let k in o) {
if (new RegExp(`(${k})`).test(str)) {
let stry = String(o[k])
str = str.replace(RegExp.$1, RegExp.$1.length === 1 ? stry : ('00' + stry).substr(stry.length))
}
}
return str
}
export function excelDataFormat(json, forma) {
let data = []
for (let i = 0; i < json.length; i++) {
const jd = json[i]
let obj = {}
for (const key in forma) {
if (forma[key] == 'Results') {
obj[key] = jd[forma[key]] ? '达标' : '未达标'
} else if (forma[key] == 'AnswerType') {
obj[key] =
jd[forma[key]] == '1'
? '纯文本'
: jd[forma[key]] == '2'
? 'URL链接'
: jd[forma[key]] == '3'
? '图片信息'
: jd[forma[key]] == '4'
? '视频音频信息'
: '链接与描述'
} else if (forma[key] == 'I') {
obj[key] = jd[forma[key]] == '-1' ? '--' : jd[forma[key]]
} else {
obj[key] = jd[forma[key]] ? jd[forma[key]] : ''
}
}
data.push(obj)
}
return data
}
4.vue文件使用
点击查看代码
<template>
<div>
<Button @click="exportHandleClick">导出</Button>
<DownExcel ref="downExcel"></DownExcel>
</div>
</template>
<script>
import { excelDataFormat, formatDate } from '../../utils/tool'
import DownExcel from '../../components/downExcel/downExcel'
export default {
components: {
DownExcel
},
data(){
return {
tableData: [
{
id: 1,
EscapeName: '122107工作面火灾避灾路线',
DisasterType_Text: '火灾',
Location: '[{"x":109.84383812459045,"y":38.623645834572514},{"x":109.85748794186557,"y":38.61695210008022}]',
Status: '0'
},
{
id: 2,
EscapeName: '122107工作面火灾避灾路线',
DisasterType_Text: '火灾',
Location: '[{"x":109.84383812459045,"y":38.623645834572514},{"x":109.85748794186557,"y":38.61695210008022}]',
Status: '0'
},
{
id: 3,
EscapeName: '122107工作面火灾避灾路线',
DisasterType_Text: '火灾',
Location: '[{"x":109.84383812459045,"y":38.623645834572514},{"x":109.85748794186557,"y":38.61695210008022}]',
Status: '1'
},
{
id: 4,
EscapeName: '122107工作面火灾避灾路线',
DisasterType_Text: '火灾',
Location: '[{"x":109.84383812459045,"y":38.623645834572514},{"x":109.85748794186557,"y":38.61695210008022}]',
Status: '1'
}
],
}
},
methos: {
exportHandleClick() {
console.log('导出')
let foramText = {
路线名称: 'EscapeName',
灾害类型: 'DisasterType_Text',
路线坐标: 'Location',
状态: 'Status'
}
let list = excelDataFormat(this.tableData, foramText)
this.$refs.downExcel.downloadExl(list, '', formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss') + '避灾路线数据', {
w: 300
})
},
}
}
</script>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了