11/09/2008

How to lock Excel Sheet generated by ASP.NET from editing ? Prevent user from selecting cells in Excel using ASP.NET !

A day back I had a requirement where user should not be able to edit the Excel sheet generated by my ASP.NET application . The user should not be even able to select cells .Normally in Excel Sheet , we can achieve this by going to Tools -- > Protect Sheet in Office 2003 , Review --> Protect Sheet in office 2007 . But How to do this in ASP.NET ? How to protect Excel sheet from getting edited using ASP.NET ? How to lock Excel Sheet using ASP.NET ? For achieving it , Use the code below .

xlWorkSheet.Cells.Locked = true;
// Locks cells in a excel Sheet
xlWorkSheet.EnableSelection =
Microsoft.Office.Interop.Excel.XlEnableSelection.xlUnlockedCells;
// Restrics selecting to only unlocked cells
xlWorkSheet.Protect("admin",false,true,true,false,false,false,false,false,false,false,false,false,false,false,false);
// Protects you Excel sheet with unprotect Password admin and attributes
// similar to Checkboxes as shown in the figure .


Add the above code just before saving your Excel workbook . Now export your excel and try to open it . You will get a message like "Your excel is Protected and readonly . To unlock it blah blah blah .... you will be prompted for password....." .


You might love to read this ? How to remove 'Sys' is undefined ?


Cool na !! For more ASP.NET solutions , Subscribe to my feed or click here to get updates via email

3 comments:

If you're like most modern consumers, you probably can’t afford to spend an entire paycheck on a replica handbags . For high fashion on a budget, turn to Top 1 Handbags. We specialize in meticulous replications of some of the most popular replica louis vuitton , wallets, replica bags , and other accessories. Each replica handbags is meticulously crafted to ensure years of reliable, handsome service.

nice article..Realy helpful

in my web application i need to export data to excel from datatable in a particular format

this has been done

i need to lock some cells range to prevent editing in excel sheet

i tried but it doesnot get locked

ExcApp = New Excel.Application

ExcWS = ExcApp.Worksheets("SHEET1")

ExcWS.Range("A1", "C3").Locked = True

ExcWS.Protect(False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False)

i have above

2) i need to display checkbox against each supplier no displayed in the sheet

& when few of the suppliers are checked , need to import the checked data in the database

Post a Comment