问题
I'm using the Microsoft.Office.Interop.Excel to open an password protected Excel file from my C++/CLI program. I supply a password as the 5th argument. When I run my program, Excel starts and opens my file but it prompts for password. Note that the file is password protected, not the worksheets.
According to the documentation the password should be of type object but supplying it as a literal should also work?
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);
回答1:
Changing the readonly argument (3rd) to true actually does the trick (except that it opens in readonly):
exApp->Workbooks->Open(filename, Type::Missing, true, ...
来源:https://stackoverflow.com/questions/29204623/opening-password-protected-excel-file-in-c-or-c-sharp-prompts-for-password-eve