• 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  自带了解密功能,
    // 不过只支持 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);
    })()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    读取加密的 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});
    })()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    使用其他的word读取库也是一样的道理,先使用 officecrypto-tool 解密以后再用对应的库去处理

  • 相关阅读:
    解决mysql Packet for query is too large
    MySQL索引设计原则
    AI 人工智能介绍(一)
    【LeetCode】每日一题 2023_11_21 美化数组的最少删除数(贪心/模拟)
    集成华为AGC认证服务实用教程-MacOS
    windows 配置多版本Java环境,无脑一把梭,浅显易懂
    VS Code快速实现Git PR操作
    Linux常用操作集合(三)
    模拟批量转换和报警功能块(博途SCL源代码)
    tiup cluster check
  • 原文地址:https://blog.csdn.net/Tyrannoaurus/article/details/132697633