首页 > 编程语言 >Node.js 使用 officecrypto-tool 读取加密的 Excel 和 Word 文档, 支持 xlsx 和 docx 文件的加密

Node.js 使用 officecrypto-tool 读取加密的 Excel 和 Word 文档, 支持 xlsx 和 docx 文件的加密

时间:2023-09-06 10:23:44浏览次数:42  
标签:Node xlsx 加密 await output input const

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

传送门:officecrypto-tool

读取加密的 Excel 示例

一: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);
 })()


二:@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'});
})()

三: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);
})()

四: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,xlsx,加密,await,output,input,const
From: https://www.cnblogs.com/whh666/p/17681579.html

相关文章

  • xlsx和path的运用
    从后端获取Excel模板app.get('/api/download-template',(req,res)=>{consttemplatePath=path.join(__dirname,'./datafile/example.xlsx');res.download(templatePath);});在前端页面,表单采用Elementui设计。利用axios接收Excel模板文件URL。填写表单,将......
  • Node.js 使用 officecrypto-tool 读取加密的 Excel (xls, xlsx) 和 Word( docx)文档
    Node.js使用officecrypto-tool读取加密的Excel(xls,xlsx)和Word(docx)文档,还支持xlsx和docx文件的加密(具体使用看文档)。暂时不支持doc文件的解密传送门:officecrypto-tool读取加密的Excel示例一:xlsx-populate//只支持xlsx,xlsx-populate自带了解密功能,/......
  • Node 配合 webseket 订阅 pgsql 的数据表变化
    pg订阅传送门服务端代码const{Client}=require('pg');constWebSocket=require('ws');constwss=newWebSocket.Server({port:8080,perMessageDeflate:false,verifyClient:(info,cb)=>{constorigin=info.origin||'......
  • Java常见的加密方式
    前言传说在古罗马时代,发生了一次大战。正当敌方部队向罗马城推进时,古罗马皇帝凯撒向前线司令官发出了一封密信:VWRSWUDIILF。这封密信被敌方情报人员翻遍英文字典,也查不出这两个词的意思。此时古罗马皇帝同时又发出了另一个指令:“前进三步”。然后古罗马军队司令官根据第二个指......
  • node.Js
    创建Node.js应用步骤一、使用require指令来加载和引入模块语法格式如下:constmodule=require('module-name');其中,module-name可以是一个文件路径(相对或绝对路径),也可以是一个模块名称,如果是一个模块名称,Node.js会自动从node_modules目录中查找该模块。require指令会返回......
  • 加密市场底部特征明显!避险情绪正主导市场走势?
       据8月的经济数据,美国经济终于出现了一些减速的迹象。美国7月CPI同比上升3.2%,虽结束了连续12个月的回落,但仍低于市场预期。核心CPI同比上升4.7%,也略低于预期。这表明通胀压力在一定程度上有所增加,但增速仍不太高。    此外在就业方面,非农业部门新增就业人数为18.7万,低于市......
  • 加密算法总结
    AESAdvancedEncryptionStandard对称加密算法RSARivest-Shamir-Adleman非对称加密算法DESDataEncryptionStandard对称加密算法ECCEllipticCurveCryptography椭圆曲线密码;非对称加密算法CMACCipher-basedMessageAuthenticationCode基于分组密码的消息认证码算法SHASecureHa......
  • ABAP 实现AES加密
    1.Github下载工具类https://github.com/Sumu-Ning/AES,利用abapGit上传至SAP系统2.AES加密需要四个参数①密钥②加密模式③字节填充模式④偏移量3.将明文转XSTRING,密钥BASE64解码,调用zcl_aes_utility=>encrypt_xstring方法METHODaes_encrypt.DATA:lv_content_xstrT......
  • Navicat里面数据库进行加密操作
    1、新建查询在里面将password字段使用md5函数进行加密:updateresetpassword=MD5(password)表格结果如下:2、在管理系统的mapper里面的sql语句进行修改然后再进行用户添加,添加进去的密码的数据就自动加密啦!3、读取密码数据登录的话,还是需要还原成为原来的密码就行判断......
  • 圣天诺Sentinel加密狗复制:保障数据安全的重要工具
    在当今这个数字化时代,数据已经成为企业最重要的资产之一。然而,随着网络gj的不断升级和数据泄露事件的频发,保障数据安全已经成为所有企业必须面对的挑战。为了应对这一挑战,越来越多的企业开始采用圣天诺Sentinel加密狗,它是保障数据安全的重要工具。圣天诺Sentinel加密狗是一种基于......