Node.js使用officecrypto-tool實現(xiàn)讀取加密的Excel和Word文檔
Node.js 使用 officecrypto-tool
讀取加密的 Excel (xls, xlsx) 和 Word( docx)文檔, 還支持 xlsx 和 docx 文件的加密(具體使用看文檔)。暫時不支持doc文件的解密
讀取加密的 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 直接內(nèi)置了解密功能,完全兼容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 進行了封裝,里面還是調(diào)用 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 示例
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 解密以后再用對應(yīng)的庫去處理
到此這篇關(guān)于Node.js使用officecrypto-tool實現(xiàn)讀取加密的Excel和Word文檔的文章就介紹到這了,更多相關(guān)Node.js讀取加密文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nodejs npm錯誤Error:UNKNOWN:unknown error,mkdir ''D:\Develop\n
今天小編就為大家分享一篇關(guān)于nodejs npm錯誤Error:UNKNOWN:unknown error,mkdir 'D:\Develop\nodejs\node_global'at Error,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03Nodejs中Express 常用中間件 body-parser 實現(xiàn)解析
這篇文章主要介紹了Nodejs中Express 常用中間件 body-parser 實現(xiàn)解析,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05Nodejs使用fs-extra模塊進行目錄和文件操作用法示例
fs-extra模塊是基于fs?的文件操作相關(guān)工具庫,封裝了一些fs實現(xiàn)起來相對復(fù)雜的工具,下面這篇文章主要給大家介紹了關(guān)于Nodejs使用fs-extra模塊進行目錄和文件操作用法的相關(guān)資料,需要的朋友可以參考下2024-06-06淺談node使用jwt生成的token應(yīng)該存在哪里
早上逛某乎的時候,遇到一位同學(xué)在問這個問題,很好奇jwt的存儲位置。本文詳細的介紹一下,感興趣的可以了解一下2021-06-06Node.js之HTTP服務(wù)端和客戶端實現(xiàn)方式
這篇文章主要介紹了Node.js之HTTP服務(wù)端和客戶端實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-09-09詳解使用Node.js 將txt文件轉(zhuǎn)為Excel文件
這篇文章主要介紹了詳解使用Node.js 將txt文件轉(zhuǎn)為Excel文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07