使用Node.js通过C++加密和解密Excel文件

使用Microsoft Excel

在Microsoft Excel(例如Microsoft Excel 2003)中设置文件加密设置:

  1. 工具菜单中选择选项。会出现一个对话框。
  2. 选择安全性标签。
  3. 输入密码并点击高级
  4. 选择加密类型并确认密码。

** 使用Aspose.Cells for Node.js via C++加密Excel文件**

以下示例演示了如何用 Aspose.Cells API 对Excel文件进行加密和密码保护。

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "Book1.xls");

// Instantiate a Workbook object.
// Open an excel file.
const workbook = new AsposeCells.Workbook(filePath);

// Specify XOR encryption type.
workbook.setEncryptionOptions(AsposeCells.EncryptionType.XOR, 40);

// Specify Strong Encryption type (RC4, Microsoft Strong Cryptographic Provider).
workbook.setEncryptionOptions(AsposeCells.EncryptionType.StrongCryptographicProvider, 128);

// Password protect the file.
workbook.getSettings().setPassword("1234");

// Save the excel file.
workbook.save(path.join(dataDir, "encryptedBook1.out.xls"));

** 指定密码修改选项**

下面的示例显示了如何使用Aspose.Cells API为现有文件设置修改密码 Microsoft Excel选项。

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");

// Instantiate a Workbook object.
// Open an excel file.
const workbook = new AsposeCells.Workbook(path.join(dataDir, "Book1.xls"));

// Set the password for modification.
workbook.getSettings().getWriteProtection().setPassword("1234");

// Save the excel file.
workbook.save(path.join(dataDir, "SpecifyPasswordToModifyOption.out.xls"));

** 使用Aspose.Cells for Node.js via C++解密Excel文件**

很容易用 Aspose.Cells API 打开受密码保护的Excel文件并解密,如以下代码所示:

const path = require("path");
const AsposeCells = require("aspose.cells.node");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "Book1.xlsx");

// Open encrypted file with password.
const loadOptions = new AsposeCells.LoadOptions();
loadOptions.setPassword("password");
const workbook = new AsposeCells.Workbook(filePath, loadOptions);

// Remove password.
workbook.getSettings().setPassword(null);

// Save the file.
workbook.save(filePath);

高级主题