React實(shí)現(xiàn)預(yù)覽展示docx和Excel文件
更新時間:2024年02月03日 16:14:26 作者:郭_昊
這篇文章主要為大家詳細(xì)介紹了如何使用React實(shí)現(xiàn)預(yù)覽展示docx和Excel文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
React預(yù)覽docx文件
封裝DocxView組件,用于顯示docx文件的預(yù)覽,支持加載loading效果
安裝依賴
npm i docx-preview
import React, { useEffect, useRef, useState } from 'react'
import * as docx from 'docx-preview'
import { Spin } from 'antd'
import { askDocApiUrls } from 'src/shared/url-map'
export interface Props {
fileInfo: string
}
const DocxView = (props: Props) => {
const { fileInfo } = props
const [isLoading, setIsLoading] = useState<boolean>(true)
const docxContainerRef = useRef<HTMLDivElement | null>(null)
useEffect(() => {
const fetchData = async () => {
try {
const response = await fetch(fileInfo)
const data = await response.blob()
const containerElement = docxContainerRef.current
if (containerElement) {
docx.renderAsync(data, containerElement).then(() => {
console.info('docx: finished')
setIsLoading(false)
})
}
} catch (error) {
setIsLoading(false)
console.error('Error fetching or rendering document:', error)
}
}
fetchData()
}, [fileInfo])
return (
<div className="relative h-full">
<div ref={docxContainerRef} className="h-full" />
{isLoading && (
<div className="absolute inset-0 flex items-center justify-center bg-white bg-opacity-75">
<Spin size="large" />
</div>
)}
</div>
)
}
export default DocxViewReact預(yù)覽展示excel文件
封裝了ExcelView來展示excel文件,支持顯示loading
1.安裝依賴
npm i @js-preview/excel
2.源碼
import React, { useEffect, useRef, useState } from 'react'
import jsPreviewExcel, { JsExcelPreview } from '@js-preview/excel'
import '@js-preview/excel/lib/index.css'
import { Spin } from 'antd'
export interface Props {
fileInfo: string
}
const ExcelView = (props: Props) => {
const { fileInfo } = props
const excelContainerRef = useRef<HTMLDivElement | null>(null)
const excelPreviewerRef = useRef<JsExcelPreview | null>(null) // 保存 myExcelPreviewer 的引用
const [isLoading, setIsLoading] = useState<boolean>(true)
useEffect(() => {
const containerElement = excelContainerRef.current
if (containerElement && !excelPreviewerRef.current) {
// 初始化 myExcelPreviewer,并保存引用
const myExcelPreviewer = jsPreviewExcel.init(containerElement)
excelPreviewerRef.current = myExcelPreviewer
setIsLoading(true) // 開始加載時設(shè)置 loading 狀態(tài)
myExcelPreviewer
.preview(fileInfo)
.then(() => {
setIsLoading(false) // 預(yù)覽完成后取消 loading 狀態(tài)
console.info('預(yù)覽完成')
})
.catch((e) => {
setIsLoading(false) // 預(yù)覽失敗后取消 loading 狀態(tài)
console.info('預(yù)覽失敗', e)
})
}
}, [fileInfo])
return (
<div className="relative h-full">
<div ref={excelContainerRef} className="h-full" />
{isLoading && (
<div className="absolute inset-0 flex items-center justify-center bg-white bg-opacity-75">
<Spin size="large" />
</div>
)}
</div>
)
}
export default ExcelView
以上就是React實(shí)現(xiàn)預(yù)覽展示docx和Excel文件的詳細(xì)內(nèi)容,更多關(guān)于React預(yù)覽文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
React父組件調(diào)用子組件中的方法實(shí)例詳解
最近做一個React項(xiàng)目,所有組件都使用了函數(shù)式組件,遇到一個父組件調(diào)用子組件方法的問題,下面這篇文章主要給大家介紹了關(guān)于React父組件調(diào)用子組件中方法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
如何使用Next.js?+?Prisma?+?MySQL開發(fā)全棧項(xiàng)目
在Next.js項(xiàng)目的任何地方都可以使用Prisma來訪問數(shù)據(jù)庫,Prisma提供了豐富的查詢方法,滿足您在項(xiàng)目中的各種需求,這篇文章主要介紹了如何使用Next.js+Prisma+MySQL開發(fā)全棧項(xiàng)目的相關(guān)資料,需要的朋友可以參考下2026-03-03
React?錯誤邊界Error?Boundary使用示例解析
這篇文章主要為大家介紹了React?錯誤邊界Error?Boundary使用示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
React中用@符號編寫文件路徑實(shí)現(xiàn)方法介紹
在Vue中,我們導(dǎo)入文件時,文件路徑中可以使用@符號指代src目錄,極大的簡化了我們對路徑的書寫。但是react中,要想實(shí)現(xiàn)這種方式書寫文件路徑,需要寫配置文件來實(shí)現(xiàn)2022-09-09

