在 C++ 或 C# 中打开受密码保护的 Excel 文件提示输入密码,即使我已经传递了密码参数

Opening password protected Excel file in C++ or C# prompts for password even though I've passed an argument for password

我正在使用 Microsoft.Office.Interop.Excel 从我的 C++/CLI 程序打开受密码保护的 Excel 文件。我提供密码作为第 5 个参数。当我 运行 我的程序时, Excel 启动并打开我的文件,但它提示输入密码。请注意,该文件受密码保护,而不是工作表。

根据文档,密码应为对象类型,但将其作为文字提供也应该有效吗?

    using namespace System;
    using namespace Microsoft::Office::Interop::Excel;
    Microsoft::Office::Interop::Excel::Application^ exApp= gcnew Microsoft::Office::Interop::Excel::ApplicationClass();
    String^ filename="e:\test.xls";
    Workbook^ wb = exApp->Workbooks->Open(filename, Type::Missing, Type::Missing, Type::Missing, "passw1", Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing, Type::Missing);

将只读参数(第 3 个)更改为 true 实际上可以解决问题(除了它以只读方式打开):

exApp->Workbooks->Open(filename, Type::Missing, true, ...