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读取加密文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Node.js安装及npm国内镜像配置的方法实现

    Node.js安装及npm国内镜像配置的方法实现

    本文主要介绍了Node.js安装及npm国内镜像配置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • 详解nodejs微信公众号开发——6.自定义菜单

    详解nodejs微信公众号开发——6.自定义菜单

    这篇文章主要介绍了详解nodejs微信公众号开发——6.自定义菜单,自定义菜单能够帮助公众号丰富界面,让用户更好更快地理解公众号的功能。
    2017-04-04
  • node+axios实现服务端文件上传示例

    node+axios实现服务端文件上传示例

    这篇文章主要介绍了node+axios实现服务端文件上传示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • node指定内存上限简单代码实例

    node指定内存上限简单代码实例

    NodeJS启动的应用,内存使用是有上限的,下面这篇文章主要给大家介绍了关于node指定内存上限的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-11-11
  • 浅谈手写node可读流之流动模式

    浅谈手写node可读流之流动模式

    这篇文章主要介绍了浅谈手写node可读流之流动模式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • nodejs+express最简易的连接数据库的方法

    nodejs+express最简易的连接数据库的方法

    这篇文章主要介绍了nodejs+express 最简易的连接数据库,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • Node.js服务器开启Gzip压缩教程

    Node.js服务器开启Gzip压缩教程

    开启网站的 gzip 压缩功能,通常可以高达70%,也就是说,如果你的网页有30K,压缩之后就变成9K, 对于大部分网站,显然可以明显提高浏览速度(注:需要浏览器支持)。
    2017-08-08
  • 把Node.js程序加入服务实现随机启动

    把Node.js程序加入服务实现随机启动

    这篇文章主要介绍了把Node.js程序加入服务实现随机启动,本文使用qckwinsvc实现这个需求,讲解了qckwinsvc的安装和使用,需要的朋友可以参考下
    2015-06-06
  • Nodejs实现的操作MongoDB数据库功能完整示例

    Nodejs实现的操作MongoDB数据库功能完整示例

    这篇文章主要介绍了Nodejs实现的操作MongoDB数据库功能,结合完整实例形式分析了nodejs针对MongoDB数据库的连接及增删改查基本操作技巧,需要的朋友可以参考下
    2019-02-02
  • node-sass安装失败的原因与解决方法

    node-sass安装失败的原因与解决方法

    这篇文章主要给大家介绍了关于node-sass安装失败的原因与解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-09-09

最新评论