docx、pptx、xlsx、pdf文件预览

docx、pptx、xlsx、pdf 文件预览

纯前端vue预览

pdf 预览

(一)pdf.js 👍🏻

GitHub - mozilla/pdf.js

最新版 pdf.js 要求 node 版本不低于 18,可以在 Tags · mozilla/pdf.js · GitHub 下载 2. 版本

具体可以看 pdf预览 | Hu Yu

(二)iframe

一般的浏览器都默认支持预览 pdf

<iframe src="viewFileUrl" />

但是不同的浏览器的展示和交互不同,而且如果 PDF 文件过大也有可能导致浏览器卡死或者白屏。

docx 预览

Github - VolodymyrBaydalka/docxjs

使用方法:

import { renderAsync } from 'docx-preview'

interface DocxOptions {
  bodyContainer?: HTMLElement | null
  styleContainer?: HTMLElement
  buffer: Blob
  docxOptions?: Partial<Record<string, string | boolean>>
}

export const renderDocx = (options: DocxOptions): Promise<void> | undefined => {
  if (typeof window !== 'undefined') {
    const { bodyContainer, styleContainer, buffer, docxOptions = {} } = options
    const defaultOptions = {
      className: 'docx',
      ignoreLastRenderedPageBreak: false,
    }
    const configuration = Object.assign({}, defaultOptions, docxOptions)
    if (bodyContainer) {
      return renderAsync(buffer, bodyContainer, styleContainer, configuration)
    } else {
      const contain = document.createElement('div')
      document.body.appendChild(contain)
      return renderAsync(buffer, contain, styleContainer, configuration)
    }
  }
}

xlsx 预览

GitHub - 501351981/vue-office

支持word(.docx)、excel(.xlsx)、pdf等各类型office文件预览的vue组件集合,提供一站式office文件预览方案,支持vue2和3,也支持React等非Vue框架。

vue-office-demo

更多用法可以看官网,比较详细

PPT 预览

暂时没有什么好的解决方法,可以使用下面的服务端预览

服务端预览

kkFileView 👍🏻

kkFileView - 在线文件预览

KkFileView 为文件文档在线预览解决方案,该项目使用流行的 spring boot 搭建,易上手和部署,基本支持主流办公文档的在线预览,如 doc, docx, xls, xlsx, ppt, pptx, pdf, txt, zip, rar, 图片, 视频, 音频等等

注意:KkFileView 为后端部署,前端直接使用后端给的地址即可

其他

WPS WebOffice 开放平台

WebOffice 开放平台

支持在线预览编辑,多人协同

阿里云 IMM

智能媒体管理-阿里云帮助中心

支持文件转换预览,但是需要与 阿里云OSS 一起使用

Office Web Viewer

微软官方提供的文档示例
demo_Sutter.pptx
微软提供的免费在线预览服务,用法简单,但是国内访问速度较慢

  • 15
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以帮你回答这个问题。首先需要获取到android11以上的存储权限,然后使用Storage Access Framework(SAF)来遍历文件。接着,我们可以使用DocumentFile API来过滤出指定类型的文件,最后使用lod.d来打印文件内容。 以下是一个示例代码: ```java // 获取存储权限 if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1); return; } // 打开SAF文件选择器 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); startActivityForResult(intent, 2); // 在onActivityResult中处理文件选择结果 @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 2 && resultCode == RESULT_OK) { Uri treeUri = data.getData(); DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri); // 遍历文件并打印 for (DocumentFile file : pickedDir.listFiles()) { String fileName = file.getName(); if (fileName.endsWith(".txt") || fileName.endsWith(".doc") || fileName.endsWith(".docx") || fileName.endsWith(".pdf") || fileName.endsWith(".ppt") || fileName.endsWith(".pptx") || fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) { try { InputStream inputStream = getContentResolver().openInputStream(file.getUri()); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { stringBuilder.append(line); } Log.d("FileContents", stringBuilder.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } } } ``` 请注意,上述代码仅供参考,并且可能需要根据您的实际需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值