Range range= (Range)this.workSheet.Cells[1,1];
range.AllowEdit = false;
当我设置AllowEdit
属性设置为 false,编译错误将显示:
错误:属性或索引器
无法将“Microsoft.Office.Interop.Excel.Range.AllowEdit”分配给
-- 它是只读的
如何将单元格范围设置为只读?
当我对此范围使用验证时,我的 Cell ContentChanged Event 出现了一些异常。
这是 CellContentChanged 中的代码:
var updater = new Action<StyleColorItem>(
item =>
{
var editedItem = _taskViewModel.TrackedItems.First(it => it.Id == item.Id);
// Above line I am getting the exception like "Sequence contains no matching element"
editedItem.Update(item);'
});
Excel 中无法将单元格设置为只读。
您可以在 C# 代码中做的是,定义一个变量或列表中的“只读”单元格,订阅 SheetChange 事件,在 SheetChange 事件中,如果该单元格发生更改,只需撤消该操作改变。
Example私有列表 readOnlyCells = new List();
private void OnActiveSheetCellChange(object changedSheet, Excel.Range changedCell)
{
if (readOnlyCells.Contains(changedCell))
changedCell.Value = string.Empty;
//.... YOUR CODE
Update
另一种选择是使用数据验证:
changedCell.Validation.Add(Excel.XlDVType.xlValidateCustom, Type.Missing, Type.Missing, "\"\"");
使用它你将拥有更少的控制权并且单元将根本不接受任何输入。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)