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

Node.js 使用 officecrypto-tool 读取加密的 Excel (xls, xlsx) 和 Word( docx)文档

时间:2023-09-05 18:22:04浏览次数:41  
标签:Node xlsx docx const officeCrypto await output input

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,docx,const,officeCrypto,await,output,input
From: https://www.cnblogs.com/zurmokeeper/p/17680477.html

相关文章

  • Node 配合 webseket 订阅 pgsql 的数据表变化
    pg订阅传送门服务端代码const{Client}=require('pg');constWebSocket=require('ws');constwss=newWebSocket.Server({port:8080,perMessageDeflate:false,verifyClient:(info,cb)=>{constorigin=info.origin||'......
  • node.Js
    创建Node.js应用步骤一、使用require指令来加载和引入模块语法格式如下:constmodule=require('module-name');其中,module-name可以是一个文件路径(相对或绝对路径),也可以是一个模块名称,如果是一个模块名称,Node.js会自动从node_modules目录中查找该模块。require指令会返回......
  • 泛微E-office getFolderZtreeNodes SQL注入漏洞
    漏洞简介getFolderZtreeNodes.php存在SQL注入漏洞,攻击者可利用该漏洞获取数据库敏感信息漏洞复现fofa语法:app="泛微-EOffice"登录页面如下:POC:POST/general/system/file_folder/purview_new/getFolderZtreeNodes.phpHTTP/1.1Host:User-Agent:Mozilla/5.0(WindowsNT......
  • Win7系统使用高版本node.js
    一、问题描述最近一段时间,由于公司前段构建Vue项目时,使用的是16.1.0版本的Node.js,而个人电脑上安装的是1.016.3导致使用VueUI时,总是提示下图的问题   于是,只好升级Node.js的版本到16.1.0,但是在升级的时候,由于我本人使用的是Win7系统,出现了Thisapplicatonisonlysupp......
  • node_exporter+Prometheus+Grafana 的数据展示
    对性能测试来说,操作系统资源的监控也是必不可少的。以node_exporter+Prometheus+Grafana为例来说下。结构图: 一、node_exporterhttps://kgithub.com/prometheus/node_exporter/releases下载安装包,解压运行即可。默认监听9100端口,注意端口冲突! 二、Prometheus下载解压Pr......
  • doc to docx ;docx to doc
    importzipfileimportosimportshutilimporthashlibimportsend2trash'''假设所有的word文档存放在某路径中,这个路径中包含各种杂七杂八的玩意使用Everything,或者"筛选文件.py"把所有的docx文件移动到C:\\Users\\asuka\\Desktop\\123逐个解压每个docx文档,并提取......
  • Node为nuxt.js 的源代码打包的执行命令
    为Node源代码打包的执行命令#清理缓存数据/root/.nvm/versions/node/v14.17.6/bin/npmcacheclean--force#删除之前使用过的插件模板文件等,若下面有.nuxt文件也一下做删除rm-rfnode_modulespackage-lock.json.nuxt#设置淘宝的npm源/root/.nvm/versions/node/v14.17......
  • sass-loader以及node-sass版本问题造成启动项目失败
    SyntaxError:ValidationError:Invalidoptionsobject.SassLoaderhasbeen报错解决方法解决方法是:更新sass-loader以及node-sass的版本。安装命令:npminstallsass-loader+版本         npminstallnode-sass+版本安装后重启项目即可......
  • nodejs + superagent 示例记录【2023-09-02】【尝试nodejs接口测试库】
    constsuperagent=require("superagent");(async()=>{ try{  constres=awaitsuperagent.get(   "https://jsonplaceholder.typicode.com/users"  );  constheaderDate=   res.headers&&res.headers.date?......
  • How to automatically run a scheduled task every hour in Node.js All In One
    HowtoautomaticallyrunascheduledtaskeveryhourinNode.jsAllInOne如何在Node.js中每间隔一小时自动运行一个定时任务引用场景Node.js后台爬虫服务,定时爬去指定页面,抓取最新数据,并写入到数据库中;在同一个Node.js部署环境中,没有使用Linux的crontab权......