3 changed files with 79 additions and 1 deletions
@ -0,0 +1,63 @@ |
|||||||
|
<script setup> |
||||||
|
import * as XLSX from 'xlsx' |
||||||
|
import { ref, onMounted } from 'vue' |
||||||
|
import { getToken } from '@/utils/token' |
||||||
|
|
||||||
|
const props = defineProps({ |
||||||
|
src: { |
||||||
|
type: String, |
||||||
|
required: true |
||||||
|
} |
||||||
|
}) |
||||||
|
|
||||||
|
const html = ref('') |
||||||
|
const loading = ref(true) |
||||||
|
|
||||||
|
onMounted(async () => { |
||||||
|
try { |
||||||
|
const res = await fetch(props.src, { |
||||||
|
headers: { |
||||||
|
Authorization: getToken() |
||||||
|
} |
||||||
|
}) |
||||||
|
|
||||||
|
const buffer = await res.arrayBuffer() |
||||||
|
const workbook = XLSX.read(buffer, { type: 'array' }) |
||||||
|
|
||||||
|
const sheet = workbook.Sheets[workbook.SheetNames[0]] |
||||||
|
html.value = XLSX.utils.sheet_to_html(sheet) |
||||||
|
} catch (e) { |
||||||
|
html.value = '<p>xls 文件解析失败,请下载查看</p>' |
||||||
|
console.error(e) |
||||||
|
} finally { |
||||||
|
loading.value = false |
||||||
|
} |
||||||
|
}) |
||||||
|
</script> |
||||||
|
|
||||||
|
<template> |
||||||
|
<div class="xls-preview" v-if="!loading" v-html="html"></div> |
||||||
|
</template> |
||||||
|
|
||||||
|
<style> |
||||||
|
.xls-preview { |
||||||
|
background: #fff; |
||||||
|
padding: 16px; |
||||||
|
color: #333; |
||||||
|
} |
||||||
|
|
||||||
|
.xls-preview table { |
||||||
|
border-collapse: collapse; |
||||||
|
width: 100%; |
||||||
|
background: #fff; |
||||||
|
} |
||||||
|
|
||||||
|
.xls-preview td, |
||||||
|
.xls-preview th { |
||||||
|
border: 1px solid #dcdfe6; |
||||||
|
padding: 6px 10px; |
||||||
|
font-size: 14px; |
||||||
|
color: #333 !important; |
||||||
|
background: #fff !important; |
||||||
|
} |
||||||
|
</style> |
||||||
Loading…
Reference in new issue