在C#中使用密码锁打开Excel工作表 [英] Open Excel Worksheet with Password lock in C#

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

问题描述

嗨伙计们,



目前我遇到了以下问题,我需要你的帮助。



但首先让我解释一下:



我有一个Excel工作簿,其中大部分单元格是只读的,并用密码锁定。你可以打开工作簿并编辑一些单元格,从只读单元格中删除。



我的应用程序现在应该通过这个excel文件并搜索特定的字符串并替换它与另一个字符串。

字符串,我正在搜索的字符串总是在可写单元格中。从不在只读单元格中。因此,打开工作簿,搜索这些字符串并替换它们,应该很容易发生而不需要密码。



但是这不起作用。所以我想,好吧,让我输入密码并将其添加到代码中:



Hi folks,

currently I stuck in the following problem and I need your help here please.

But first let me explain it:

I have an Excel Workbook where most Cells are read-only and locked with a password. You can open the workbook and edit some cells, exept from the read-only cells.

My application now should go through this excel file and search a specific string and replace it with another string.
The string, what I'm searching for is always in the writable cells. Never in the read-only cells. So opening the workbook, search for those strings and replace them, should easily happen without the need of the password.

But this did not work. So I thought, ok, just let me enter the password and add it in the code:

Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook;
xlWorkBook = xlApp.Workbooks.Open(filePath.ToString(), 0, false, 5, "myPassword", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);







但这也行不通。我得到类似的东西:

命令无法应用于受保护的工作表。



所以,我怎么能实现这一点;编辑受保护的工作簿并保存更改。如果可能的话,如果保存的工作簿也会有一些只读(受保护)单元格,我会很感激吗?





提前谢谢

Max




But this also doesn't work. I'm getting something like:
"The command cannot be applied to a protected sheet."

So, how could I achieve this; to edit a protected workbook and save the changes. If possible I would appreciate if the saved workbook will also got some read-only (protected) cells?


Thanks in advance
Max

推荐答案

您需要取消保护要编辑的工作表

喜欢

sheet1.unprotect(密码);
You Need to unprotect Sheet which you want to edit
like
sheet1.unprotect(Password);


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

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