0

I need to lock a particular cell value in an Excel file using OfficeWriter. Is this possible? If so, what API calls do I need to use?

AlisonB
  • 395
  • 1
  • 6
Prabakaran
  • 221
  • 4
  • 9
  • 19
  • 3
    If this question is about a broken CAPS-LOCK key, it should go to Superuser.com. – Thilo Oct 08 '13 at 04:53
  • @Thilo: Hey this is about to locking cells in excel using office writer – Prabakaran Oct 08 '13 at 05:08
  • 1
    I SEE. SORRY I GOT CONFUSED. – Thilo Oct 08 '13 at 05:10
  • 1
    Another user posted a similar question that may be of help: http://stackoverflow.com/questions/18905045/excelwriter-style-celllocked-why-wont-it-work -Note: I work for SoftArtisans, makers of OfficeWriter. – AlisonB Oct 09 '13 at 11:34
  • @AlisonB Hi, thanks for your comments. I am using the OW version 3.8. where i can get tutorial for that version. Because i the wiki they have provided code samplels using latest API. could you please help me? – Prabakaran Oct 10 '13 at 01:29
  • There is no difference between OfficeWriter 3.8 and OfficeWriter 8 in regards to Worksheet.Protect and Style.CellLocked. The code samples for the latest version will work with 3.8. The only difference is that 3.8 only supports XLS files, rather than XLSX. 3.8 may also contain bugs that were resolved. The OfficeWriter change log will outline any bug fixes that were made after 3.8: http://wiki.softartisans.com/display/OW8/Change+Log. – AlisonB Oct 15 '13 at 13:10

1 Answers1

3

Locking a cell with OfficeWriter is the same as it is in Excel: all cells have a 'locked' property as part of their style. The locked property is set to true by default, but does not take affect until the worksheet is protected.

ExcelApplication xla = new ExcelApplication();
Workbook wb = xla.Create(ExcelApplication.FileFormat.Xlsx);
Worksheet ws = wb.Worksheets[0];

//Protecting the sheet will lock any cells 
//that have the locked property set to true
ws.Protect("MyPassword");

To leave certain cells unlocked, create a style where the locked property is set to false. Then apply the style to the cells you wish to leave unlocked.

ExcelApplication xla = new ExcelApplication();
Workbook wb = xla.Create(ExcelApplication.FileFormat.Xlsx);
Worksheet ws = wb.Worksheets[0];

//Create the 'unlocked' style
Style unlockedStyle = wb.CreateStyle();
unlockedStyle.CellLocked = false;

//Apply to any cells you wish to leave unlocked
//when the worksheet is protected
ws.Cells["A1"].Style = unlockedStyle;
ws.Cells["B1"].Style = unlockedStyle;


//Protecting the sheet will lock any cells 
//that have the locked property set to true
ws.Protect("MyPassword");

We have some additional related information on protecting worksheets in our documentation: http://wiki.softartisans.com/display/EW8/Protecting+Your+Worksheet

Note, I work for SoftArtisans, makers of OfficeWriter.

AlisonB
  • 395
  • 1
  • 6