electron客户端预览doc、docx、excel、pdf、ppt、csv、txt等文件类型

需求:已知文件路径,通过点击列表页的操作按钮可查看文件内容。

提供思路:实现方式有两种:

(1)打开弹窗或客户端新开窗口。不同类型应用不同插件进行直接预览查看。

(2)通过electron的shell.openPath指定系统默认程序直接打开页面。(这种方式实现简单,样式规范,具有统一性)

这里介绍第二种实现方式:

主进程监听:main.js

javascript 复制代码
const { shell } = require('electron')
// 通过系统打开窗口预览文件 
ipcMain.on('open-file', (event, filePath) => {
  // 打开文件
  shell
    .openPath(filePath)
    .then(() => {
      console.log('文件已使用系统默认程序打开')
    })
    .catch(err => {
      console.error('打开文件失败:', err)
    })
})

渲染进程preload.js

javascript 复制代码
  // 打开窗口预览文件
  openFileSend: (...args) => ipcRenderer.send("open-file", ...args),

vue组件使用:

javascript 复制代码
function previewFile (row) {
  // const filePath = 'D:\\work\\test.csv' 调试用
  window.electronAPI?.openFileSend(row.cached_file_path)
}
相关推荐
织_网19 小时前
Electron 核心 API 全解析:从基础到实战场景
前端·javascript·electron
hweiyu0020 小时前
Python办公之Excel(openpyxl)、PPT(python-pptx)、Word(python-docx)
python·powerpoint·excel
java技术之路20 小时前
【免费AI文档助手开发实战系列】基于正则表达式的PDF脱敏python服务构建(一)
python·pdf·pymupdf·免费pdf脱敏
l1t1 天前
DeepSeek辅助编写的将xlsx格式文件中sheet1.xml按需分别保留或去掉标签的程序
xml·python·excel·wps·xlsx
耘田2 天前
Markdown to PDF/PNG Converter
pdf
揭老师高效办公2 天前
在Excel和WPS表格中为多个数字同时加上相同的数值
excel·wps表格
zooooooooy2 天前
Electron实现自定义全量更新
spring boot·electron
zzc9212 天前
Wireshark导出数据包时间和长度
wireshark·excel·导出·csv·time·length·波形
Goona_2 天前
PyQt多窗口应用开发:构建完整的可二次开发用户登录注册模板
python·小程序·excel·pyqt
爪洼守门员2 天前
安装electron报错的解决方法
前端·javascript·electron