这里的图片地址是本地盘符里的图片,我用nginx配成了服务
data(){
return{
imageList:[],
xlselist:[
['姓名’,'年纪','邮箱'],
['张三','18','要跳转的地址'],
['张三1','19','要跳转的地址'],
['张三2','20','要跳转的地址'],
['张三3','21','要跳转的地址'],
]
}
}
down(){
// 创建一个 jsZip 实例
const zip = new JSZip()
//表格xlsx文件
const data = this.xlselist
// 创建 workbook 和 worksheet 对象
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.aoa_to_sheet(data);
// 遍历数据源中的每一行(除第一行外)
for (let i = 1; i < data.length; i++) {
// 获取当前行号
const rowIndex = i + 1;
ws[`B${rowIndex}`].s = null // 先将单元格的s属性设为null
// 设置超链接样式
ws[`B${rowIndex}`].s = {
font: { color: { rgb:'#0000FF'}, underline: true },
};
// 设置超链接
const linkText = `${data[i][1]}`;
ws[`B${rowIndex}`].v = linkText;
ws[`B${rowIndex}`].l = { Target: data[i][1], Tooltip: linkText };
}
// 将 worksheet 添加到 workbook 中
XLSX.utils.book_append_sheet(wb, ws, "sheet1")
// 导出Excel, 注意这里用到的是XLSXS对象
const wbout = XLSX.write(wb, { bookType: 'xlsx', type: 'binary' });
let thexlsl = new Blob([this.s2ab(wbout)], { type: 'application/octet-stream' })
zip.file("数据信息.xlsx", thexlsl)
// 遍历图片数组,将每张图片添加到文件夹
this.imageList.forEach((imgBase64, index) => {
const imgUrl = imgBase64.url
const imgBlob = atob(imgUrl)
const imgArray = []
for (let i = 0; i < imgBlob.length; i++) {
imgArray.push(imgBlob.charCodeAt(i))
}
const imgFile = new Blob([new Uint8Array(imgArray)], { type: 'image/png' })
// 将图片添加进 img文件中
let img =zip.folder("img")
// 将图片添加进 zip 包
img.file(imgBase64.name, imgFile)
})
// 生成 zip 文件并下载
zip.generateAsync({ type: 'blob' }).then(content => {
saveAs(content, mmdd()+this.searchdata.centre+'.zip')
})
}
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
},
//选择图片是把路径转成base64
selecet(val){
for(let i=0;i<val.length;i++){
let obj={}
const imagePath =this.$imgurl+ val[i].picPath
// console.log(imagePath);
const xhr = new XMLHttpRequest()
xhr.open("GET", imagePath,true)
xhr.responseType = 'blob'
xhr.onload = () => {
const reader = new FileReader()
reader.readAsDataURL(xhr.response)
reader.onload = () => {
obj.url=reader.result.split(",")[1]
}
}
xhr.send()
obj.name=val[i].name
this.imageList.push(obj)
} }