Node.js使用officecrypto-tool实现读取加密的Excel和Word文档

 更新时间:2023年09月06日 09:32:14   作者:神话  
这篇文章主要为大家详细介绍了Node.js如何使用officecrypto-tool实现读取加密的Excel和Word文档的功能,感兴趣的小伙伴可以跟随小编一起了解一下

Node.js 使用 officecrypto-tool 读取加密的 Excel (xls, xlsx) 和 Word( docx)文档, 还支持 xlsx 和 docx 文件的加密(具体使用看文档)。暂时不支持doc文件的解密

传送门:officecrypto-tool

读取加密的 Excel 示例

1.xlsx-populate 

// 只支持 xlsx, xlsx-populate  自带了解密功能,
// 不过只支持 ecma376 agile 模式,也就是Office 生成的加密的docx,
// WPS的就不行, WPS用的是 ecma376 standard 模式
const XlsxPopulate = require('xlsx-populate');
(async ()=>{
    const input = await fs.readFile(`pass_test.xlsx`);
    const output = await officeCrypto.decrypt(input, {password: '123456'});
    const workbook = await XlsxPopulate.fromDataAsync(output);
    // 或者可先判断文件是否是加密的
    const isEncrypted = officeCrypto.isEncrypted(input);
    let output = input;
    if (isEncrypted) {
        output = await officeCrypto.decrypt(input, {password: '123456'});
    }
    const workbook = await XlsxPopulate.fromDataAsync(output);
 })()

2.@zurmokeeper/exceljs

https://www.npmjs.com/package/@zurmokeeper/exceljs

// 只支持 xlsx @zurmokeeper/exceljs 直接内置了解密功能,完全兼容exceljs v4.3.0
const Excel = require('@zurmokeeper/exceljs');
(async ()=>{
    // 从文件读取, 解密使用密码加密的excel文件
    const workbook = new Excel.Workbook();
    await workbook.xlsx.readFile(filename, {password:'123456'});
    // 从流读取, 解密使用密码加密的excel文件
    const workbook = new Excel.Workbook();
    await workbook.xlsx.read(stream, {password:'123456'});
    // 从 buffer 加载, 解密使用密码加密的excel文件
    const workbook = new Excel.Workbook();
    await workbook.xlsx.load(data, {password:'123456'});
})()

3.xlsx

// xlsx 支持 xls 和 xlsx
const XLSX = require('xlsx');
(async ()=>{
    const input = await fs.readFile(`pass_test.xlsx`);
    // const input = await fs.readFile(`pass_test.xls`); // 或者xls
    const output = await officeCrypto.decrypt(input, {password: '123456'});
    const workbook = XLSX.read(output);
    // 或者可先判断文件是否是加密的
    const isEncrypted = officeCrypto.isEncrypted(input);
    let output = input;
    if (isEncrypted) {
        output = await officeCrypto.decrypt(input, {password: '123456'});
    }
    const workbook = XLSX.read(output);
})()

4.node-xlsx

// 其实 node-xlsx 只是对xlsx 进行了封装,里面还是调用 xlsx 去解析的
const nodeXlsx = require('node-xlsx');
(async ()=>{
    const input = await fs.readFile(`pass_test.xlsx`);
    // const input = await fs.readFile(`pass_test.xls`); // 或者xls
    const output = await officeCrypto.decrypt(input, {password: '123456'});
    const workbook = nodeXlsx.parse(output);
    // 或者可先判断文件是否是加密的
    const isEncrypted = officeCrypto.isEncrypted(input);
    let output = input;
    if (isEncrypted) {
        output = await officeCrypto.decrypt(input, {password: '123456'});
    }
    const workbook = nodeXlsx.parse(output);
})()

读取加密的 Word 示例

使用:mammoth officecrypto-tool

const officeCrypto = require('officecrypto-tool');
const fs = require('fs').promises;
const mammoth = require('mammoth');
(async ()=>{
    const input = await fs.readFile(`pass_test.xlsx`);
    const output = await officeCrypto.decrypt(input, {password: '123456'});
    await mammoth.convertToHtml({buffer: output});
    // 或者可先判断文件是否是加密的
    const isEncrypted = officeCrypto.isEncrypted(input);
    let output = input;
    if (isEncrypted) {
        output = await officeCrypto.decrypt(input, {password: '123456'});
    }
    await mammoth.convertToHtml({buffer: output});
})()

使用其他的word读取库也是一样的道理,先使用 officecrypto-tool 解密以后再用对应的库去处理

到此这篇关于Node.js使用officecrypto-tool实现读取加密的Excel和Word文档的文章就介绍到这了,更多相关Node.js读取加密文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • nodejs语言实现验证码生成功能的示例代码

    nodejs语言实现验证码生成功能的示例代码

    这篇文章主要介绍了nodejs语言实现验证码生成功能的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • nodejs npm错误Error:UNKNOWN:unknown error,mkdir ''D:\Develop\nodejs\node_global''at Error

    nodejs npm错误Error:UNKNOWN:unknown error,mkdir ''D:\Develop\n

    今天小编就为大家分享一篇关于nodejs npm错误Error:UNKNOWN:unknown error,mkdir 'D:\Develop\nodejs\node_global'at Error,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • Nodejs中Express 常用中间件 body-parser 实现解析

    Nodejs中Express 常用中间件 body-parser 实现解析

    这篇文章主要介绍了Nodejs中Express 常用中间件 body-parser 实现解析,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • Nodejs使用fs-extra模块进行目录和文件操作用法示例

    Nodejs使用fs-extra模块进行目录和文件操作用法示例

    fs-extra模块是基于fs 的文件操作相关工具库,封装了一些fs实现起来相对复杂的工具,下面这篇文章主要给大家介绍了关于Nodejs使用fs-extra模块进行目录和文件操作用法的相关资料,需要的朋友可以参考下
    2024-06-06
  • 浅谈node使用jwt生成的token应该存在哪里

    浅谈node使用jwt生成的token应该存在哪里

    早上逛某乎的时候,遇到一位同学在问这个问题,很好奇jwt的存储位置。本文详细的介绍一下,感兴趣的可以了解一下
    2021-06-06
  • 如何在命令行判断node.js启动了没有(最新)

    如何在命令行判断node.js启动了没有(最新)

    这篇文章主要介绍了如何在命令行判断node.js启动了没有,使用 tasklist 命令列出所有正在运行的进程,并使用 findstr 命令过滤出 Node.js 进程,感兴趣的朋友跟随小编一起看看吧
    2024-06-06
  • Node.js之HTTP服务端和客户端实现方式

    Node.js之HTTP服务端和客户端实现方式

    这篇文章主要介绍了Node.js之HTTP服务端和客户端实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-09-09
  • npm下载慢或下载失败问题解决的三种方法

    npm下载慢或下载失败问题解决的三种方法

    这篇文章主要为大家介绍了npm下载慢或下载失败问题解决的三种方法,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • 详解使用Node.js 将txt文件转为Excel文件

    详解使用Node.js 将txt文件转为Excel文件

    这篇文章主要介绍了详解使用Node.js 将txt文件转为Excel文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Node.js打包管理工具NPM用法

    Node.js打包管理工具NPM用法

    这篇文章介绍了Node.js打包管理工具NPM的用法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04

最新评论