vue3在线预览excel表格

在 Vue 3 项目中实现 Excel 表格在线预览。

**使用 @vue-office/excel(支持 .xlsx/.xls)**‌

该库专为 Vue 3 设计,支持 Word、Excel、PPT、PDF 等多种格式,集成简单、样式保留较好。

安装依赖

npm install @vue-office/excel @vue-office/docx @vue-office/pptx @vue-office/pdf

引入样式(重要)

import '@vue-office/excel/lib/index.css'

使用组件

<template>
<div class="file-preview-container">
<VueOfficeExcel
v-if="['xlsx', 'xls'].includes(fileType)"
:src="fileSrc"
:style="{ width: '100%', height: '600px', border: '1px solid #e8e8e8' }"
@rendered="handleRendered"
@error="handleError"
/>
</div>
</template>

<script setup>
import { ref, computed } from 'vue'
import VueOfficeExcel from '@vue-office/excel'

const props = defineProps({
src: String // 可为 URL、Blob、File 或 ArrayBuffer
})

const fileSrc = ref(props.src)
const fileType = computed(() => {
if (!props.src) return ''
const filename = typeof props.src === 'string' ? props.src.toLowerCase() : props.src.name?.toLowerCase()
if (filename.endsWith('.xlsx')) return 'xlsx'
if (filename.endsWith('.xls')) return 'xls'
return ''
})

const handleRendered = () => console.log('Excel 渲染完成')
const handleError = (err) => console.error('渲染失败:', err)
</script>

✅ 优点:支持 .xls.xlsx,保留原始样式,无需手动解析。

⚠️ 注意:需确保文件来源支持跨域,或通过后端代理获取二进制流 ‌27。

相关推荐
史迪仔01128 小时前
[QML] QML IMage图像处理
开发语言·前端·javascript·c++·qt
AI_Claude_code8 小时前
ZLibrary访问困境方案四:利用Cloudflare Workers等边缘计算实现访问
javascript·人工智能·爬虫·python·网络爬虫·边缘计算·爬山算法
ManageEngineITSM9 小时前
IT服务台为什么越忙越低效?
人工智能·自动化·excel·itsm·工单系统
Cobyte9 小时前
3.响应式系统基础:从发布订阅模式的角度理解 Vue2 的数据响应式原理
前端·javascript·vue.js
竹林81810 小时前
从零到一:在React前端中集成The Graph查询Uniswap V3池数据实战
前端·javascript
军军君0110 小时前
Three.js基础功能学习十八:智能黑板实现实例五
前端·javascript·vue.js·3d·typescript·前端框架·threejs
Moment10 小时前
AI全栈入门指南:一文搞清楚NestJs 中的 Controller 和路由
前端·javascript·后端
禅思院10 小时前
前端架构演进:基于AST的常量模块自动化迁移实践
前端·vue.js·前端框架
程序员马晓博10 小时前
前端并发治理:从 Token 刷新聊起,一个 Promise 就够了
前端·javascript