react预览word文档和Excel文档

react预览word文档和Excel文档
yarn add react-file-viewer或者npm install react-file-viewer
import FileViewer from ‘react-file-viewer’;

const [fileDataBlob, setFileDataBlob] = useState();
const [fileDataBlobflag, setFileDataBlobflag] = useState(false);

const data = await downloadFile({ id: info?.id }) //获取文件二进制流 blob,最好不要直接用url,很有可能会报跨域
const href = URL.createObjectURL(data)
setFileDataBlob(href)
setFileDataBlobflag(true)
在这里插入图片描述
这页面Html
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vue 中预览 Word 文件和 Excel 文件,可以通过引入第三方插件实现。 1. 首先,需要安装并引入 `office-ui-fabric-js` 和 `office-ui-fabric-react` 这两个依赖库。 ```bash npm install office-ui-fabric-js office-ui-fabric-react --save ``` 2. 然后,可以通过 `office-ui-fabric-react` 提供的 `DocumentCard` 和 `DocumentCardPreview` 组件来实现 Word 和 Excel 文件的预览。需要注意的是,这两个组件需要在 `componentDidMount` 生命周期中加载。 ```vue <template> <div> <DocumentCard> <DocumentCardPreview previewImages={[ { previewIconProps: { iconName: 'WordDocument' }, previewProps: { previewFileType: PreviewFileType.Document, previewFileUrl: 'http://example.com/word.docx' } } ]} /> </DocumentCard> <DocumentCard> <DocumentCardPreview previewImages={[ { previewIconProps: { iconName: 'ExcelWorkbook' }, previewProps: { previewFileType: PreviewFileType.Document, previewFileUrl: 'http://example.com/excel.xlsx' } } ]} /> </DocumentCard> </div> </template> <script> import { DocumentCard, DocumentCardPreview, PreviewFileType } from 'office-ui-fabric-react' export default { name: 'FilePreview', mounted () { require('office-ui-fabric-js/dist/css/fabric.min.css') require('office-ui-fabric-js/dist/css/fabric.components.min.css') }, components: { DocumentCard, DocumentCardPreview } } </script> <style scoped> /* 样式可以根据需求自行修改 */ </style> ``` 3. 最后,需要在后端将 Word 和 Excel 文件转换为 PDF 格式,然后在前端通过 `iframe` 标签来展示 PDF 文件。 ```vue <template> <div> <iframe :src="pdfUrl" frameborder="0" width="100%" height="600"></iframe> </div> </template> <script> export default { name: 'FilePreview', data () { return { pdfUrl: '' } }, mounted () { this.getPdfUrl() }, methods: { getPdfUrl () { // 从后端获取 PDF 文件的 URL // ... this.pdfUrl = 'http://example.com/file.pdf' } } } </script> <style scoped> /* 样式可以根据需求自行修改 */ </style> ``` 以上是两种实现 Word 和 Excel 文件预览的方法,可以根据自己的需求选择合适的方式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值