Node.js使用officecrypto-tool實(shí)現(xiàn)讀取加密的Excel和Word文檔
Node.js 使用 officecrypto-tool 讀取加密的 Excel (xls, xlsx) 和 Word( docx)文檔, 還支持 xlsx 和 docx 文件的加密(具體使用看文檔)。暫時(shí)不支持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
// 其實(shí) node-xlsx 只是對(duì)xlsx 進(jìn)行了封裝,里面還是調(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 解密以后再用對(duì)應(yīng)的庫去處理
到此這篇關(guān)于Node.js使用officecrypto-tool實(shí)現(xiàn)讀取加密的Excel和Word文檔的文章就介紹到這了,更多相關(guān)Node.js讀取加密文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Node.js安裝及npm國內(nèi)鏡像配置的方法實(shí)現(xiàn)
本文主要介紹了Node.js安裝及npm國內(nèi)鏡像配置,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
詳解nodejs微信公眾號(hào)開發(fā)——6.自定義菜單
這篇文章主要介紹了詳解nodejs微信公眾號(hào)開發(fā)——6.自定義菜單,自定義菜單能夠幫助公眾號(hào)豐富界面,讓用戶更好更快地理解公眾號(hào)的功能。2017-04-04
node+axios實(shí)現(xiàn)服務(wù)端文件上傳示例
這篇文章主要介紹了node+axios實(shí)現(xiàn)服務(wù)端文件上傳示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
nodejs+express最簡易的連接數(shù)據(jù)庫的方法
這篇文章主要介紹了nodejs+express 最簡易的連接數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
把Node.js程序加入服務(wù)實(shí)現(xiàn)隨機(jī)啟動(dòng)
這篇文章主要介紹了把Node.js程序加入服務(wù)實(shí)現(xiàn)隨機(jī)啟動(dòng),本文使用qckwinsvc實(shí)現(xiàn)這個(gè)需求,講解了qckwinsvc的安裝和使用,需要的朋友可以參考下2015-06-06
Nodejs實(shí)現(xiàn)的操作MongoDB數(shù)據(jù)庫功能完整示例
這篇文章主要介紹了Nodejs實(shí)現(xiàn)的操作MongoDB數(shù)據(jù)庫功能,結(jié)合完整實(shí)例形式分析了nodejs針對(duì)MongoDB數(shù)據(jù)庫的連接及增刪改查基本操作技巧,需要的朋友可以參考下2019-02-02

