Open EXCEL (.xlsx) with password in C#

前端 未结 2 1977
醉酒成梦
醉酒成梦 2020-12-19 21:22

I try to open password protected .xlsx files (Excel 2007 format) without typing the password manually. I have installed Excel 2003 and the Microsoft Office Comp

相关标签:
2条回答
  • 2020-12-19 21:55

    This may be late, but for any future person with Interop, for me it worked like this:

    To open to write

    var WFile = new Excel.Application();
    Excel.Workbook Wbook = WFile.Workbooks.Open("myFilepath", ReadOnly: false, Password: "mypassword");
    

    To open as read-only

    var WFile = new Excel.Application();
    Excel.Workbook Wbook = WFile.Workbooks.Open("myFilepath", ReadOnly: true, Password: "mypassword");
    
    0 讨论(0)
  • 2020-12-19 21:59

    The Answer by @J1mm1995 works when trying to open files as readonly, and fails to open some excel files when you want to open the excel file for modification(ReadOnly is set to false).

    I understood that this was because the Workbooks.Open() method also expected you to specify the WritePassword. The following code worked for me:

    var WFile = new Excel.Application();
    Excel.Workbook Wbook = WFile.Workbooks.Open(path,  ReadOnly: false, Password: "mypassword", WriteResPassword: "mypassword");
    
    
    0 讨论(0)
提交回复
热议问题