简体   繁体   中英

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 ). But it was not working. I installed MSOffice 2010 (was originally using 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. 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). 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

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

The Version of Assembly I'm using does not contain the type False . Should I upgrade to Office 2010?

The COM reference I'm using is Microsoft Office 12.0 Object Library Version 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

(2) Set the protection to not enable selection of locked cells:

myWorkSheet.EnableSelection = xlUnlockedCells 

This will prevent the user from selecting the locked cells, which then prevents them from being able to copy them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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