附件上传excel新窗口简单预览

1.下载插件xlsx     npm i xlsx

2.引入插件 import * as XLSX from "xlsx"

3.使用upload的change钩子函数获取文件file.raw

<el-upload 
    size="small" 
    ref="upload" 
    class="upload-demo" 
    action="/app/resources/upload/file"
	multiple :on-success="onSuccess" 
    :show-file-list="false" 
    :on-change="handleFileUpload"/>

 

<script>
import * as XLSX from "xlsx"
export default {
	
	data () {
		return {
			previewData: {
				headers: [],
				rows: []
			}
           }
        }
    methods:{
        handleFileUpload (file) {
			const reader = new FileReader()
			reader.onload = (e) => {
				const data = e.target.result
				const workbook = XLSX.read(data, { type: 'binary' })
				const sheetName = workbook.SheetNames[0]
				const sheet = workbook.Sheets[sheetName]
				const json = XLSX.utils.sheet_to_json(sheet, { header: 1 })
				this.previewData.headers = json[0]
				this.previewData.rows = json.slice(1)
			}
			reader.readAsBinaryString(file.raw)
		},

        //查看附件
		openPreview () {
			var encodedObject = encodeURIComponent(JSON.stringify(this.previewData));
			window.open(`/file-preview?data=` + encodedObject, '_blank');
		},
        }
    }
</script>

4.使用window的方法打开新的网页跳转传参(数据小点可以,大点的话会报错)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值