如何从C#编辑或取消保护受保护的Excel工作表中的单个单元格?

时间:2022-10-11 01:38:26

I'm trying to manipulate an excel file with C# by using Microsoft.Office.Interop.Excel. The workbook I'm trying to manipulate has shared protection between users.

我正在尝试使用Microsoft.Office.Interop.Excel使用C#操作excel文件。我试图操作的工作簿在用户之间共享保护。

I have the file password and I have the password of the cells which I want to access and edit.

我有文件密码,我有我想要访问和编辑的单元格的密码。

If I try to do that from Excel, I do the following:

如果我尝试从Excel执行此操作,请执行以下操作:

  • open the file, type the password
  • 打开文件,输入密码
  • go to a cell, double click on a cell to edit.
  • 转到单元格,双击单元格进行编辑。
  • a dialogue appears, asking me to provide the password, I provide the password and then I can edit.
  • 出现对话框,要求我提供密码,我提供密码然后我就可以编辑了。

I want to do this from a C# application.

我想从C#应用程序中执行此操作。

My current code is as following:

我目前的代码如下:

xl.Application excelApp = new xl.Application();
excelApp.Visible = true;
xl.Workbook newworkbook =
    excelApp.Workbooks.Open(@"C:\1.xls", 0, false, 5, "password", "", false,
                            xl.XlPlatform.xlWindows, "", true, false, 0, true,
                            false, false);
xl.Sheets excelSheets = newworkbook.Worksheets;
xl.Worksheet excelWorksheet = (xl.Worksheet)excelSheets.get_Item("Sign On_Off");
excelWorksheet.Select(true);
xl.Range myrange = excelWorksheet.get_Range("b16", "b16");
myrange.Value2 = "testing"; 

The last line gives me this error message:

最后一行给出了以下错误消息:

The cell or chart that you are trying to change is protected and therefore read-only.

您尝试更改的单元格或图表受到保护,因此是只读的。

I can't unprotect the whole sheet cause that would mean I will have the file opened exclusively for me only and other users can't save changes.

我不能解除整个工作表的原因,这意味着我将只为我打开文件,其他用户无法保存更改。

So my question: Is is there is way I can unprotect a cell only?

所以我的问题是:我是否有办法取消保护细胞?

I imagine something like:

我想象的是:

myrange.unprotect("pw");

2 个解决方案

#1


1  

Protect is just available to Workbook and Worksheet classes.

保护仅适用于Workbook和Worksheet类。

Alternatively, you can try to work with Worksheet.Protection Property:

或者,您可以尝试使用Worksheet.Protection属性:

ActiveSheet.Protection.AllowEditRanges.Add _
    Title:="Range123", Range:=Range("K4:L10"), Password:="123"

#2


1  

I haven't had time to try it, but this paragraph looks like what you are looking for. It seems that the VBA equivalent object you are looking for is ActiveSheet.Protection.AllowEditRanges, which stores information about the areas that can be edited by users in a sheet.
One thing you might want to look into as well is the UserInterfaceOnly option on ActiveSheet.Protect. You can protect a range with a password for users, but access it without password through macros.

我没有时间尝试,但这段看起来像你在寻找。您正在寻找的VBA等效对象似乎是ActiveSheet.Protection.AllowEditRanges,它存储有关用户可以在工作表中编辑的区域的信息。您可能想要研究的一件事是ActiveSheet.Protect上的UserInterfaceOnly选项。您可以使用用户密码保护范围,但可以通过宏访问密码而不使用密码。

#1


1  

Protect is just available to Workbook and Worksheet classes.

保护仅适用于Workbook和Worksheet类。

Alternatively, you can try to work with Worksheet.Protection Property:

或者,您可以尝试使用Worksheet.Protection属性:

ActiveSheet.Protection.AllowEditRanges.Add _
    Title:="Range123", Range:=Range("K4:L10"), Password:="123"

#2


1  

I haven't had time to try it, but this paragraph looks like what you are looking for. It seems that the VBA equivalent object you are looking for is ActiveSheet.Protection.AllowEditRanges, which stores information about the areas that can be edited by users in a sheet.
One thing you might want to look into as well is the UserInterfaceOnly option on ActiveSheet.Protect. You can protect a range with a password for users, but access it without password through macros.

我没有时间尝试,但这段看起来像你在寻找。您正在寻找的VBA等效对象似乎是ActiveSheet.Protection.AllowEditRanges,它存储有关用户可以在工作表中编辑的区域的信息。您可能想要研究的一件事是ActiveSheet.Protect上的UserInterfaceOnly选项。您可以使用用户密码保护范围,但可以通过宏访问密码而不使用密码。