使用C#保护excel中的列

我试图写入Excel表格中的一些数据,并在写入必要的数据后,我想通过使它们不可编辑来防止单元格编辑。 在这个操作之后,一些数据将会在页面用户的不受保护的单元格上被更新,然后我将使用它们并读取它们来执行一些处理活动。

  1. 有人可以帮我一个具体的,如何locking在Excel中编辑一列?
  2. 你如何写C#到一个受保护的列?
  3. 你如何阅读从受保护的列使用C#?

简单的参考例子将大大帮助我。 PS:如果你对VB.NET更加熟悉,甚至参考如何做到这一点将是有益的。

对于Excel单元格处于不可编辑模式,必须注意以下两点:

1)Excel单元格应该被locking

ws.get_Range(“Location”,Type.Missing).Locked = true;

2)Excel工作表也应该被locking

ws.Protect("SecurityCode", true, true, true, Type.Missing, Type.Missing, true, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); //where ws is the worksheet object 

对于你的第二个问题来读取一个受保护的单元格,这可以直接完成

 string CellValue = ws.get_Range("Location", Type.Missing).Value2.ToString(); //Now depending upon the CellValue you can write your own Logic.