简体   繁体   English

如何使用C#代码为Excel中的所有单元格禁用剪切/复制选项?

[英]How to disable cut/copy option for all cells in Excel using C# code?

/***** --EDIT-- SOLUTION It is really weird how it finally worked out. / *****- 编辑- 解决方案最终解决方案真的很奇怪。 The Interop Assembly I was using contained the methods to disable selection of the cells ( App.EnableSelection ). 我正在使用的Interop程序集包含禁用单元格选择的方法( App.EnableSelection )。 But it was not working. 但这没有用。 I installed MSOffice 2010 (was originally using 2007). 我安装了MSOffice 2010(最初使用的是2007)。 Now, with the new version of assembly installed, and with no change in code at all, the excel sheet is behaving as expected, both, on Excel2007 and Excel2010. 现在,安装了新版本的Assembly,并且根本没有更改代码,Excel表格在Excel2007和Excel2010上的行为均符合预期。 I'm still not able to comprehend what happened at the assembly level, but it finally solved my problem. 我仍然无法理解在组装级别发生的情况,但是最终解决了我的问题。 :) *****/ :) ***** /

I'm writing a WPF application which programatically generates an excel file(which is a salary report). 我正在编写一个WPF应用程序,该程序以编程方式生成一个excel文件(这是一份工资报告)。 I've protected the file using the following code 我已使用以下代码保护了文件

myWorkSheet.Protect(Password);

The protection works fine but, I don't want the user to be able to copy any cell data to another sheet. 该保护工作正常,但是,我不希望用户能够将任何单元格数据复制到另一张纸上。 I've tried using the following code but it doesn't work 我尝试使用下面的代码,但是它不起作用

myWorkSheet.Application.CutCopyMode = (Excel.XlCutCopyMode)0;

I'm still able to copy cell data. 我仍然能够复制单元格数据。 What is going wrong? 怎么了?

NOTE: The environment is .NET Framework 4.5, MSOffice 2007, Microsoft.Office.Interop Assembly Version 1.6.0.0 注意:环境是.NET Framework 4.5,MSOffice 2007,Microsoft.Office.Interop程序集版本1.6.0.0

What is really weird is that Application.CutCopyMode has types False , xlCut , and xlCopy [1]: https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._application.cutcopymode.aspx 真正奇怪的是Application.CutCopyMode具有类型FalsexlCutxlCopy [1]: https : xlCopy

The Version of Assembly I'm using does not contain the type False . 我正在使用的程序集版本不包含False类型。 Should I upgrade to Office 2010? 我应该升级到Office 2010吗?

The COM reference I'm using is Microsoft Office 12.0 Object Library Version 1.6. 我使用的COM参考是Microsoft Office 12.0对象库版本1.6。

I've been suggested to lock the cells and disable the selection on them, to prevent the user from copying cell data. 建议我锁定单元格并禁用对它们的选择,以防止用户复制单元格数据。 Here is the code, but it doesn't work. 这是代码,但是不起作用。 Don't know why. 不知道为什么。

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

//Adding some data to the sheet, modifying cells alignments,adding      border,headers

xlWorkSheet.UsedRange.Cells.Locked = true;
xlWorkSheet.Protect("Lomesh", misValue, true, misValue, true, misValue,
misValue, misValue, misValue, misValue, misValue, misValue, misValue,
misValue, misValue, misValue);

xlWorkSheet.EnableSelection = Excel.XlEnableSelection.xlUnlockedCells;

The above code does not work. 上面的代码不起作用。 I'm still able to copy cell data. 我仍然能够复制单元格数据。

//Saving using save dialog

xlWorkBook.SaveCopyAs(path);

xlWorkBook.Close(true, misValue, misValue);



xlApp.Quit();

Protecting the sheet is not enough. 仅保护板料还不够。 You also have to: 您还必须:

(1) Lock the cells, if they are not already locked, and (1)锁定单元格(如果尚未锁定),以及

(2) Set the protection to not enable selection of locked cells: (2)将保护设置为不启用对锁定单元格的选择:

myWorkSheet.EnableSelection = xlUnlockedCells 

This will prevent the user from selecting the locked cells, which then prevents them from being able to copy them. 这将防止用户选择锁定的单元格,从而阻止它们复制它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM