Node.js 使用 officecrypto-tool 读取加密的 Excel 和 Word 文档, 支持 xlsx 和 docx 文件的加密 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
zurmokeeper
V2EX    程序员

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

  •  
  •   zurmokeeper
    zurmokeeper 2023-09-06 09:30:28 +08:00 1225 次点击
    这是一个创建于 774 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 解密以后再用对应的库去处理

    目前尚无回复
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2661 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 73ms UTC 03:15 PVG 11:15 LAX 20:15 JFK 23:15
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86