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连接mysql数据库简单封装示例-mysql模块

    nodejs连接mysql数据库简单封装示例-mysql模块

    本篇文章主要介绍了nodejs连接mysql数据库简单封装(mysql模块),具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-04-04
  • Node.js如何自动审核团队的代码

    Node.js如何自动审核团队的代码

    在项目开发中,统一团队的代码风格很重要,本文介绍如何用Node.js来自动审核,来提高您的开发速度。
    2016-07-07
  • 利用adb shell和node.js实现抖音自动抢红包功能(推荐)

    利用adb shell和node.js实现抖音自动抢红包功能(推荐)

    这篇文章主要介绍了利用adb shell和node.js实现抖音自动抢红包功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2018-02-02
  • 详解node如何让一个端口同时支持https与http

    详解node如何让一个端口同时支持https与http

    众所周知node是一个高性能的web服务器,使用它可以很简单的创建一个http或https的服务器。这篇文章主要介绍了详解node如何让一个端口同时支持https与http
    2017-07-07
  • Node.js中的process.nextTick使用实例

    Node.js中的process.nextTick使用实例

    这篇文章主要介绍了Node.js中的process.nextTick使用实例,nextTick函数有什么用、怎么用、和setTimeout有什么区别呢,本文就讲解了这些知识,需要的朋友可以参考下
    2015-06-06
  • node.js学习之base64编码解码

    node.js学习之base64编码解码

    开发者对Base64编码肯定很熟悉,是否对它有很清晰的认识就不一定了。实际上Base64已经简单到不能再简单了,这篇文章给大家通过示例代码介绍了node.js对字符串和图片base64编码解码的方法,有需要的朋友们可以通过本文来进行学习,下面来一起看看吧。
    2016-10-10
  • Node学习记录之cluster模块

    Node学习记录之cluster模块

    本篇文章主要介绍了Node学习记录之cluster模块,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • node.js实现为PDF添加水印的示例代码

    node.js实现为PDF添加水印的示例代码

    这篇文章主要介绍了node.js实现为PDF添加水印的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-12-12
  • Node.js Stream ondata触发时机与顺序的探索

    Node.js Stream ondata触发时机与顺序的探索

    今天小编就为大家分享一篇关于Node.js Stream ondata触发时机与顺序的探索,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • 使用Node.js给图片加水印的方法

    使用Node.js给图片加水印的方法

    使用Node.js给图片加水印,首先要确保本地安装了node环境。然后,我们进行图像编辑操作需要用到一个Node.js的库:images。具体详情大家可以通过本文了解下
    2016-11-11

最新评论