博客摘录「 node版本如何升级」2024年1月23日

<template> <div> <button @click="previewFile">Preview Excel</button> <iframe ref="previewIframe" v-if="showPreview" :src="previewUrl" width="100%" height="500"></iframe> </div> </template> <script> import XLSX from 'xlsx'; export default { data() { return { showPreview: false, previewUrl: '', }; }, methods: { async previewFile() { try { // 获取文件内容 const fileContent = await this.getFileContent(); // 使用 xlsx 库将 .xlsx 文件转换为 HTML const workbook = XLSX.read(fileContent, { type: 'buffer' }); const sheetName = workbook.SheetNames[0]; const sheet = workbook.Sheets[sheetName]; // 转换为 HTML 表格 const tableHtml = XLSX.utils.sheet_to_html(sheet); // 创建 HTML 文档并嵌入表格 const htmlDoc = ` <!DOCTYPE html> <html> <head> <title>Excel Preview</title> </head> <body> ${tableHtml} </body> </html> `; // 将 HTML 写入 Blob URL const blob = new Blob([htmlDoc], { type: 'text/html' }); const url = URL.createObjectURL(blob); // 更新预览 URL this.previewUrl = url; this.showPreview = true; // 清理工作 this.$refs.previewIframe.onload = () => { setTimeout(() => { URL.revokeObjectURL(url); }, 60000); // 撤销 URL 以避免内存泄漏 }; } catch (error) { console.error('Error while previewing file:', error); } }, async getFileContent() { // 这里应该返回一个 Promise,它解析为文件的 Blob 对象 // 你可以从服务器获取文件,或者让用户上传文件 // 作为示例,我们返回一个空的 Blob 对象 return new Blob(); }, }, }; </script>

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值