在C#中使用密码打开EXCEL(.xlsx) [英] Open EXCEL (.xlsx) with password in C#

查看:703
本文介绍了在C#中使用密码打开EXCEL(.xlsx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试打开受密码保护的.xlsx文件(Excel 2007格式),而无需手动输入密码.我已经安装了Excel 2003和 Microsoft Office兼容包可以在打开时转换文件.

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 Compatibility Pack that converts the file when opening.

以下代码有效,但提示输入密码.

The following code works, but it prompt for the password.

Microsoft.Office.Interop.Excel.Application ExcelApp;
Microsoft.Office.Interop.Excel.Workbook ExcelWorkbook;
ExcelApp = new Microsoft.Office.Interop.Excel.Application();           
Object pwd = "xxx";
Object MissingValue = System.Reflection.Missing.Value;
ExcelWorkbook = ExcelApp.Workbooks.Open("C:\\temp\\test.xlsx",MissingValue, MissingValue, MissingValue,pwd);

如果我使用相同的代码打开.xls文件(Excel 2003),它将在不提示输入密码的情况下工作.在没有密码保护的情况下打开.xlsx文件也可以正常工作.

If I use the same code to open a .xls File (Excel 2003), it works without prompting for the password. Opening .xlsx files without password protection also works fine.

如何在没有使用Excel 2003和Microsoft Office兼容包提示输入密码的情况下打开受密码保护的.xlsx文件?

How is it possible to open password protected .xlsx files without prompting for the password with Excel 2003 and the Microsoft Office Compatibility Pack?

来自

The trick from a similar problem changing the readonly argument (3rd) to true

ExcelWorkbook = ExcelApp.Workbooks.Open("C:\\temp\\test.xlsx",MissingValue, true, MissingValue,pwd);

在这里不起作用.

推荐答案

这可能已经晚了,但是对于将来有Interop的任何人来说,对我来说它都是这样的:

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

打开写作

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

以只读方式打开

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

这篇关于在C#中使用密码打开EXCEL(.xlsx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆