如何使用C#锁定某些excel单元格或使其成为只读 [英] How to lock certain excel cells or make them read-only using C#

查看:920
本文介绍了如何使用C#锁定某些excel单元格或使其成为只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想使用C#锁定一些excel单元格(不是全部)。

当有人打开excel时文件,他们将能够编辑除这些单元格之外的任何单元格。如果有人希望删除所有可编辑数据,那么只读单元格将保留其值,而其他可编辑单元格将丢失其值。工作簿也不应该有密码。



我试图像这样锁定单元格:



 wsht.Cells [rowcnt,colcnt] .Locked =  true ; 





以类似的方式尝试锁定范围。但他们不会工作。锁定电池或范围后保护片材使所有电池都是只读的。



任何人?



< b>我尝试了什么:



锁定单元格:

 wsht.Cells [ rowcnt,colcnt] .Locked =  true ; 



锁定范围:

< pre lang =c#> wsht.Range [wsht.Cells [rowcnt, 1 ],wsht.Cells [rowcnt,wordString.Length - 1 ]]。锁定= true ;





在这些代码使所有单元格无法编辑后,它们将无法工作并保护工作表。

解决方案

首先保护工作表,

< pre lang =c#> wsht.Protect()



然后 un 保护你想要编辑的单元格。

 wsht.Range [wsht.Cells [rowcnt, 1 ],wsht.Cells [rowcnt,wordString.Length  -   1 ]]。已锁定=  false ; 


Hi All,

I want to lock some of excel cells (not all) using C#.
When someone opens the excel file, they will be able to edit any cell except those cells. And if someone wishes to delete all editable data then read-only cells will retain their value and other editable cells will lose their value. Also the workbook should not have password.

I tried to lock cells like this:

wsht.Cells[rowcnt, colcnt].Locked = true;



Tried locking range in similar way. But they won't work. Protecting sheet after locking cells or range makes all cells read-only.

Anyone??

What I have tried:

Locking cells:

wsht.Cells[rowcnt, colcnt].Locked = true;


Locking range:

wsht.Range[wsht.Cells[rowcnt, 1], wsht.Cells[rowcnt, wordString.Length - 1]].Locked = true;



They won't work and protecting sheet after these codes makes all cells uneditable.

解决方案

Protect the sheet first,

wsht.Protect()


Then unprotect the cells you want to edit.

wsht.Range[wsht.Cells[rowcnt, 1], wsht.Cells[rowcnt, wordString.Length - 1]].Locked = false;


这篇关于如何使用C#锁定某些excel单元格或使其成为只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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